You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi 11.3中array of const字符串参数使用问题求助

问题分析及解决方法

原因分析

  1. UnicodeString类型未被正确处理
    Delphi 11.3中array of const的元素是TVarRec类型,当传入UnicodeString字面量时,对应的vType是vtUnicodeString(枚举值16)。如果你的代码只处理了旧的vtString(对应ShortString,枚举值3),就会遗漏这种情况,若误判类型值则可能误以为是_Reserved1(17)。

  2. 枚举定义不匹配
    如果代码中引用了自定义的TVarRec定义(而非System单元的标准定义),或者引入了旧版本的兼容单元,会导致vType的枚举值偏移,将实际的vtUnicodeString识别为_Reserved1(17)。

纠正方法

1. 确保使用标准TVarRec定义

确认代码引用了System单元(Delphi 11默认自动引入),避免自定义或旧版本的TVarRec覆盖系统定义。

2. 完善类型判断逻辑

在处理array of const时,增加对vtUnicodeString的判断,同时兼容其他字符串类型:

procedure MyFunc(test: array of const);
var
  i: Integer;
  strValue: string;
begin
  for i := 0 to High(test) do
  begin
    case test[i].VType of
      vtInteger:
        Writeln('整数: ', test[i].VInteger);
      vtUnicodeString:
        begin
          strValue := UnicodeString(test[i].VUnicodeString);
          Writeln('Unicode字符串: ', strValue);
        end;
      // 可选:兼容AnsiString和ShortString
      vtAnsiString:
        begin
          strValue := string(AnsiString(test[i].VAnsiString));
          Writeln('Ansi字符串: ', strValue);
        end;
      vtString:
        Writeln('短字符串: ', test[i].VString^);
      else
        Writeln('不支持的类型: ', test[i].VType);
    end;
  end;
end;

3. 检查编译选项

确保开启{$H+}编译选项(Delphi 11默认开启),此时字符串字面量默认是UnicodeString类型,无需额外转换即可直接传入:

MyFunc(['test', 9]);

内容的提问来源于stack exchange,提问作者user2277327

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 01:21:17