Delphi 11.3中array of const字符串参数使用问题求助
问题分析及解决方法
原因分析
UnicodeString类型未被正确处理
Delphi 11.3中array of const的元素是TVarRec类型,当传入UnicodeString字面量时,对应的vType是vtUnicodeString(枚举值16)。如果你的代码只处理了旧的vtString(对应ShortString,枚举值3),就会遗漏这种情况,若误判类型值则可能误以为是_Reserved1(17)。枚举定义不匹配
如果代码中引用了自定义的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
相关产品推荐
相关产品推荐

