我正在使用SysUtils.Format函数和variant值,我发现只有格式字符串才能使用此函数%s.我检查了有关Format函数的文档,但是没有任何关于如何处理变量值的引用.
考虑这个简单的应用:
{$APPTYPE CONSOLE}
uses
Variants,
SysUtils;
procedure TestFormat;
var
v : Variant;
begin
v:=100;
writeln(Format('The VarType of v is %s',[VarTypeAsText(VarType(v))]));
writeln(Format('The value of v is %s',[v]));//ok
v:='100';
writeln(Format('The VarType of v is %s',[VarTypeAsText(VarType(v))]));
writeln(Format('The value of v is %s',[v]));//ok
v:=100;
writeln(Format('The VarType of v is %s',[VarTypeAsText(VarType(v))]));
writeln(Format('The value of v is %d',[v]));//raise a EConvertError exception EConvertError: Format '%d' invalid or incompatible with argument
end;
begin
try
TestFormat;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
readln;
end.
Run Code Online (Sandbox Code Playgroud)
这是一个错误还是这个功能的简单限制?
我在Delphi 5,Delphi 2007和Delphi XE中检查过这种行为.
jac*_*ate 12
这是功能的限制.在Delphi XE中,SysUtils中的相关部分从第10870行开始,如下所示:
@CvtVariant:
CMP CL,'S'
JNE @CvtError
Run Code Online (Sandbox Code Playgroud)
这适用于任何变量参数.CL寄存器具有该特定参数的格式字符串所需的类型,对于不同于"S"的任何内容,引发异常.