This article needs additional citations for verification. (May 2019) (Learn how and when to remove this template message)
In Visual Basic (and Visual Basic for Applications) the Variant data type is a tagged union that can be used to represent any other data type (for example, integer, floating-point, single- and double-precision, object, etc.) except fixed-length string type. 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 (comparable to a tagged union), 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.
In Visual Basic, a variant named A can be declared either explicitly or implicitly:
Dim A Dim A as Variant
In Delphi, a variant named A is declared in the following way:
var A: variant;
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:
|0||2||The value returned by VarType; specifies what kind of data the variant contains.|
|2||6||Reserved bytes; should be set to zero.|
|8||up to 8||The data the variant contains.|
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.
|VarType||Hex||Propvariant Type||Propvariant Member||TypeName||Data bytes||C and C++ type|
|8||0x08||VT_BSTR||bstrVal||String||xxxxxxxx||(BSTR):(OLECHAR *):(WCHAR *):(wchar_t *)|
|10||0x0a||VT_ERROR||scode||Error||2A000A80||HRESULT (long int)|
|17||0x11||VT_UI1||bVal||Byte||2A||BYTE (unsigned char)|
|18||0x12||VT_UI2||uiVal||WORD (unsigned short)|
|19||0x13||VT_UI4||ulVal||DWORD (unsigned int)|
|25||0x19||VT_HRESULT||Missing3||80020004||HRESULT (long int)|
|9||Object reference5||xxxxxxxx||IUnknown *|
- 1 The type of an uninitialized variant.
- 2 The type of a NULL value in a database, that is, not uninitialized, nor equivalent to a C++ null pointer.
- 3 Missing arguments are actually a particular Error value titled "parameter not found".
- 4 The object type set to a null reference.
- 5 TypeName will return the name of the class of the object contained. The data would be an interface pointer, that is, a pointer to a pointer to a virtual method table (which is an array of function pointers).
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.
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(Optional ByVal Index) As String If IsMissing(Index) Then GetText = Item(CurrentItem) Else GetText = Item(Index) End If End Function
Similarly the keyword
ParamArray can be used to pass all following arguments in a variant array.