RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
Variant Class

System::Variant is a C++ implementation of the Delphi intrinsic type System::Variant.

Pascal
Variant = class(TVarData);
C++
class Variant : public TVarData;

The System::Variant type is capable of representing values that change type dynamically. Whereas a variable of any other type is statically bound to that type, a variable of the System::Variant type can assume values of differing types at run-time. The System::Variant type is most commonly used in situations where the actual type to be operated upon varies or is unknown at compile-time. While variants offer great flexibility, they also consume more memory than regular variables, and operations on variants are substantially slower than operations on statically typed values. 

System::Variants can contain various numeric, string, Boolean, and date-time values. If COM is available (Windows only) variants can also contain OLE Automation objects. Finally, variants can contain arrays of any of the above types. The special System::Variant value Unassigned is used to indicate that a variant has not yet been assigned a value, and the special variant value Null is used to indicate unknown or missing data. 

A variant can be constructed from the following data types: 

short 

int 

float 

double 

Currency 

TDateTime 

bool 

WordBool 

Byte 

AnsiString

char

wchar_t * const 

Ole2::IDispatch* const (Windows only) 

Ole2::IUnknown* const (Windows only)

Note: When a System::Variant contains an OLE Automation object, the variant can be used to get and set properties of the object, and to invoke methods on the object.
System::Variant variables are always initialized to be Unassigned when they are first created. This is true whether a variant variable is global, local, or part of a structure such as an array. 

In C++ code, variant arrays are usually created using a System::Variant constructor in conjunction with the OPENARRAY macro:

Variant V(OPENARRAY(int,(0,HighVal,0,HighVal)),varInteger);

You can also use the VarArrayCreate function, but that function is designed for use in Delphi code, and makes for complex C++ code.

Note: Use only the operators declared within System::Variant. The compiler ignores any operators you overload yourself.
Note: For binary operators, if one operand is of type System::Variant, the other operand is automatically converted to type System::Variant.
 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!