Quick start manual

5-34
Delphi Language Guide
Variant types
The standard function VarType returns a variant’s type code. The varTypeMask
constant is a bit mask used to extract the code from VarType’s return value, so that,
for example,
VarType(V) and varTypeMask = varDouble
returns True if V contains a Double or an array of Double. (The mask simply hides the
first bit, which indicates whether the variant holds an array.) The TVarData record
type defined in the System unit can be used to typecast variants and gain access to
their internal representation.
Variant type conversions
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.
Table 5.7 Variant type conversion rules
Target
Source
integer real string Boolean
integer
converts integer
formats
converts to
real
converts to
string
representation
returns False if 0, True
otherwise
real
rounds to
nearest integer
converts real
formats
converts to
string
representation
using regional
settings
returns False if 0, True
otherwise