【发布时间】:2011-07-29 09:07:54
【问题描述】:
我正在使用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.
这是这个功能的一个错误还是一个简单的限制?
我在 Delphi 5、Delphi 2007 和 Delphi XE 中检查了这种行为。
【问题讨论】: