The Delphi Language, Part 1 - Variant Types
(Page 5 of 10 )
The Variant class provides an implementation of a type capable of serving as a kind of container for many other types. This means that a variant can change the kind of data to which it refers at runtime. For example, the following code will compile and run properly:
var
V: Variant;
I: IInterface;
begin
V := 'Delphi is Great!'; // Variant holds a string
V := 1; // Variant now holds an Integer
V := 123.34; // Variant now holds a floating point
V := True; // Variant now holds a boolean
V := I; // Variant now holds an interface
end; Variants support a wide variety of types, including complex types such as arrays and interfaces. The following code from the Borland.Vcl.Variants unit shows the various supported types, which are identified by values known as TVarTypes:
type
TVarType = Integer;
const // Decimal | Other names for this type
varEmpty = $0000; // = 0 | Unassigned, Nil
varNull = $0001; // = 1 | Null, System.DBNull
varSmallInt = $0002; // = 2 | I2, System.Int16
varInteger = $0003; // = 3 | I4, System.Int32
varSingle = $0004; // = 4 | R4, System.Single
varDouble = $0005; // = 5 | R8, System.Double
varCurrency = $0006; // = 6 | Borland.Delphi.System.Currency
varDate = $0007; // = 7 | Borland.Delphi.System.TDateTime
varString = $0008; // = 8 | WideString, System.String
varError = $000A; // = 10 | Exception, System.Exception
varBoolean = $000B; // = 11 | Bool, System.Boolean
varObject = $000C; // = 12 | TObject, System.Object
varDecimal = $000E; // = 14 | System.Decimal
varShortInt = $0010; // = 16 | I1, System.SByte
varByte = $0011; // = 17 | U1, System.Byte
varWord = $0012; // = 18 | U2, System.UInt16
varLongWord = $0013; // = 19 | U4, System.UInt32
varInt64 = $0014; // = 20 | I8, System.Int64
varUInt64 = $0015; // = 21 | U8, System.UInt64
varChar = $0016; // = 22 | WideChar, System.Char
varDateTime = $0017; // = 23 | System.DateTime;
varFirst = varEmpty;
varLast = varDateTime;
varArray = $2000; // = 8192 | System.Array, Dynamic Arrays
varTypeMask = $0FFF; // = 4095
varUndefined = -1;
As a function of value assignment, variants are capable of coercing themselves into other types, as needed. For example:
var
V: Variant;
I: Integer;
begin
V := '1'; // V hold string '1'
I := V; // Implicitly converts to Integer, I is now 1
end;
Typecasting Variants
You can explicitly typecast expressions to type Variant. For example, the expression
Variant(X); results in a Variant type whose type code corresponds to the result of the expression X, which must be an integer, floating point, currency, string, character, date/time, class, or Boolean type.
You can also typecast a variant to that of a simple data type. For example, given the assignment
V := 1.6; where V is a variable of type Variant, the following expressions will have the following results shown:
S := string(V); // S will contain the string '1.6';
I := Integer(V); // I is rounded to the nearest Integer value, in this case: 2.
B := Boolean(V); // B contains True, since V is not equal to 0
D := Double(V); // D contains the value 1.6 These results are dictated by certain type-conversion rules applicable to Variant types. These rules are defined in detail in the Delphi Language Guide.
By the way, in the preceding example, it isn't necessary to typecast the variant to another data type to make the assignment. The following code would work just as well:
V := 1.6;
S := V;
I := V;
B := V;
D := V; When multiple variants are used in an expression, there can be much more behind-the-scenes code logic attached to these implicit type coercions. In such cases, if you're sure of the type a variant contains, you're better off explicitly typecasting it to that type in order to speed up the operation.
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Variants in Expressions >>
More .NET Articles
More By Xavier Pacheco