All integer, real, string, character, and Boolean types are assignment-compatible with Variant. Expressions can be explicitly cast as variants, and the VarAsType and VarCast standard routines can be used to change the internal representation of a variant. The following code demonstrates the use of variants and some of the automatic conversions performed when variants are mixed with other types.
var
V1, V2, V3, V4, V5: Variant;
I: Integer;
D: Double;
S: string;
begin
V1 := 1; { integer value }
V2 := 1234.5678; { real value }
V3 := 'Hello world!'; { string value }
V4 := '1000'; { string value }
V5 := V1 + V2 + V4; { real value
2235.5678}
I := V1; { I = 1 (integer value) }
D := V2; { D = 1234.5678 (real value) }
S := V3; { S = 'Hello world!' (string
value) }
I := V4; { I = 1000 (integer value) }
S := V5; { S = '2235.5678' (string
value) }
end;
The compiler performs type conversions according to the following rules.
Source\Target |
integer |
real |
string |
character |
Boolean |
|
integer |
converts integer formats |
converts to real |
converts to string reoresentation |
same as string (left) |
return False if 0, True otherwise |
|
Real |
rounds to nearest integer |
converts real formats |
converts to string reoresentation using regional settings |
same as string (left) |
return False if 0, True otherwise |
|
String |
converts to integer, truncating if necessary; raises exception if string is not numeric |
converts to real using regional settings; raises exception if string is not numeric |
converts string/character formats |
same as string (left) |
return False if string is False (non-case-sensitive) or a numeric string that evaluates to 0, True if string is True or a nonzero numeric string; raises exception otherwise |
|
character |
same as string (above) |
same as string (above) |
same as string (above) |
same as string-to-string |
same as string (above) |
|
Boolean |
False = 0, True = (255 if Byte) |
False = 0, True =-1 |
False =”0”, True = “-1” |
same as string (left) |
False = False, True = True |
|
Unassigned |
returns 0 |
returns 0 |
returns empty string |
same as string (left) |
return False |
|
Null |
raises exception |
raises exception |
raises exception |
same as string (left) |
raises exception |
Out-of-range
assignments often result in the target variable getting the highest value in
its range. Invalid assignments or casts raise the EVariantError
exception.
Special
conversion rules apply to the TDateTime real type declared in the System
unit. When a TDateTime is converted to any other type, it treated as a
normal Double. When an integer, real, or Boolean is converted to a TDateTime,
it is first converted to a Double, then read as a date-time value. When
a string is converted to a TDateTime, it is interpreted as a date-time
value using the regional settings. When an Unassigned value is converted
to TDateTime, it is treated like the real or integer value 0. Converting
a Null value to TDateTime raises an exception.
On Windows, if a variant references a COM interface, any attempt to convert it reads the object’s default property and converts that value to the requested type. If the object has no default property, an exception is raised.
所有整数、实数、串、字符和布尔类型对变体Variant都是赋值兼容的。表达式能被明确作为变体转换,VarAsType和VarCast标准例程可以用于改变变体的内部表示。下面的代码演示了变体与其他类型混合时的用法以及自动转换。
var
V1, V2, V3, V4, V5: Variant;
I: Integer;
D: Double;
S: string;
begin
V1 := 1; { 整数值 }
V2 := 1234.5678; { 实数值 }
V3 := 'Hello world!'; { 串值 }
V4 := '1000'; { 串值 }
V5 := V1 + V2 + V4; { 实数值 2235.5678}
I := V1; { I = 1 (整数值) }
D := V2; { D = 1234.5678 (实数值) }
S := V3; { S = 'Hello world!' (串值) }
I := V4; { I = 1000 (整数值) }
S := V5; { S = '2235.5678' (串值) }
end;
编译器根据下列规则对变体进行类型转换。
源\目标 |
整数 |
实数 |
串 |
字符 |
布尔型 |
|
整数 |
转换整数格式 |
转换为实数 |
转换到串表示 |
同串(左) |
如果为0则返回False 否则返回True |
|
实数 |
四舍五入到最近的整数 |
转换实数格式 |
用本地设置转换到串表示 |
同串(左) |
如果为0则返回False 否则返回True |
|
串 |
转换到整数,如果必需则截断;如果串不是数字则引发异常 |
用本地设置转换到实数;如果串不是数字则引发异常 |
转换串/字符格式 |
同串(左) |
如果串是False(大小写不敏感)或串是一个等于0的数字串则返回False,如果串是True或串是一个非0的数字串则返回True,其他则引发异常 |
|
字符 |
同串(上) |
同串(上) |
同串(上) |
同串到串 |
同串(上) |
|
布尔型 |
False = 0, True = (255 if Byte) |
False = 0, True =-1 |
False =”0” True = “-1” |
同串(左) |
False = False, True = True |
|
Unassigned |
返回0 |
返回0 |
返回空串 |
同串(左) |
返回False |
|
Null |
引发异常 |
引发异常 |
引发异常 |
同串(左) |
引发异常 |
越界赋值通常导致目标变量得到其范围内的最大值。非法的赋值或转换引发EVariantError异常。
在System单元中对TDateTime类型声明了特殊的转换规则。当一个TDateTime类型转换到任意其他类型时,被视为一般的Double类型。当整数、实数或布尔型转换到TDateTime类型时,首先转换为Double型,然后作为日期-时间值读取。当一个Unassigned值被转换到TDateTime类型时,它将被视为实数或整数值0。转换一个Null值到TDateTime类型时,将引发异常。
在Windows系统中,如果一个变体引用是一个COM接口,那么任何转换尝试将读取对象的缺省属性的值并将其转换为请求的类型。如果对象没有缺省属性,那么将引发异常。