Variant type


Variant is a data type in certain programming languages, particularly Visual Basic, OCaml, Delphi and C++ when using the Component Object Model.
In Visual Basic the Variant data type is a tagged union that can be used to represent any other data type except fixed-length string type and record types. In Visual Basic, any variable not declared explicitly or the type of which is not declared explicitly, is taken to be a variant.
While the use of not explicitly declared variants is not recommended, they can be of use when the needed data type can only be known at runtime, when the data type is expected to vary, or when optional parameters and parameter arrays are desired. In fact, languages with a dynamic type system often have variant as the only available type for variables.
Among the major changes in Visual Basic.NET, being a.NET language, the variant type was replaced with the.NET object type. There are similarities in concept, but also major differences, and no direct conversions exist between these two types. For conversions, as might be needed if Visual Basic.NET code is interacting with a Visual Basic 6 COM object, the normal methodology is to use.NET marshalling.
In unrelated usage, variant type is also used to refer to an algebraic data type, whose constructors are often called variants. In languages such as OCaml and Haskell, this kind of variant type is the standard language building block for representing many data structures.

Examples

In Visual Basic, a variant named A can be explicitly declared as shown in either of these two examples:

Dim A
Dim A as Variant

In Delphi, a variant named A is declared in the following way:

var A: variant;

Format

A variable of variant type, for brevity called a "variant", as defined in Visual Basic, needs 16 bytes storage and its layout is as follows:
OffsetSizeDescription
02The value returned by VarType; specifies what kind of data the variant contains.
26Reserved bytes; should be set to zero.
8up to 8The data the variant contains.

Types

A few examples of variants that one can encounter in Visual Basic follow. In other languages other kinds of variants can be used as well.
VarTypeHexPropvariant TypePropvariant MemberTypeNameData bytesC and C++ type
00x00VT_EMPTY-Empty1
10x01VT_NULL-Null2
20x02VT_I2iValInteger2A00SHORT
30x03VT_I4lValLong2A000000LONG
40x04VT_R4fltValSingle00002842FLOAT
50x05VT_R8dblValDouble0000000000004540DOUBLE
60x06VT_CYcyValCurrencyA068060000000000CY structure
70x07VT_DATEdateDate00000000C0D5E140DATE
80x08VT_BSTRbstrValStringxxxxxxxx:::
90x09VT_DISPATCHpdispVal
100x0aVT_ERRORscodeError2A000A80HRESULT
110x0bVT_BOOLboolValBooleanFFFFVARIANT_BOOL
120x0cVT_VARIANTpvarValVariantVARIANT
130x0dVT_UNKNOWNpunkValNothing400000000IUnknown *
140x0eVT_DECIMALdecValDECIMAL
160x10VT_I1cValByteCHAR
170x11VT_UI1bValByte2ABYTE
180x12VT_UI2uiValWORD
190x13VT_UI4ulValDWORD
200x14VT_I8hVal
210x15VT_UI8uhVal
220x16VT_INTintVal
230x17VT_UINTuintVal
240x18VT_VOID
250x19VT_HRESULTMissing380020004HRESULT
260x1aVT_PTR
270x1bVT_SAFEARRAYparray
280x1cVT_CARRAY
290x1dVT_USERDEFINED
300x1eVT_LPSTRpszVal
310x1fVT_LPWSTRpwszVal
360x24VT_RECORD
370x25VT_INT_PTRpintVal
380x26VT_UINT_PTRpuintVal
81920x2000VT_ARRAYparray
9Object reference5xxxxxxxxIUnknown *-

Collections

The Collection class in OLE Automation can store items of different data types. Since the data type of these items cannot be known at compile time, the methods to add items to and retrieve items from a collection use variants. If in Visual Basic the For Each construct is used, the iterator variable must be of object type, or a variant.

Dispatch method calls

In OLE Automation the IDispatch interface is used when the class of an object cannot be known in advance. Hence when calling a method on such an object the types of the arguments and the return value is not known at compile time. The arguments are passed as an array of variants and when the call completes a variant is returned.

Optional parameters

In Visual Basic a procedure argument can be declared to be optional by prefixing it with the Optional keyword. When the argument is omitted Visual Basic passes a special value to the procedure, called Missing in the table above, indicating that the argument is missing. Since the value could either be a supplied value or a special value, a variant must be used.

Function GetText As String
If IsMissing Then
GetText = Item
Else
GetText = Item
End If
End Function

Similarly the keyword ParamArray can be used to pass all following arguments in a variant array.