Variant type
Encyclopedia
Variant is a data type
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...

 in certain programming languages, particularly Visual Basic
Visual Basic
Visual Basic is the third-generation event-driven programming language and integrated development environment from Microsoft for its COM programming model...

 and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 when using the Component Object Model
Component Object Model
Component Object Model is a binary-interface standard for software componentry introduced by Microsoft in 1993. It is used to enable interprocess communication and dynamic object creation in a large range of programming languages...

.

In Visual Basic (and Visual Basic for Applications
Visual Basic for Applications
Visual Basic for Applications is an implementation of Microsoft's event-driven programming language Visual Basic 6 and its associated integrated development environment , which are built into most Microsoft Office applications...

) the Variant data type is a tagged union
Tagged union
In computer science, a tagged union, also called a variant, variant record, discriminated union, or disjoint union, is a data structure used to hold a value that could take on several different, but fixed types. Only one of the types can be in use at any one time, and a tag field explicitly...

 that can be used to represent any other data type (for example, integer
Integer (computer science)
In computer science, an integer is a datum of integral data type, a data type which represents some finite subset of the mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values....

, floating-point, single- and double
Double precision
In computing, double precision is a computer number format that occupies two adjacent storage locations in computer memory. A double-precision number, sometimes simply called a double, may be defined to be an integer, fixed point, or floating point .Modern computers with 32-bit storage locations...

-precision, object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

, etc.) except fixed-length string type and record
Record (computer science)
In computer science, a record is an instance of a product of primitive data types called a tuple. In C it is the compound data in a struct. Records are among the simplest data structures. A record is a value that contains other values, typically in fixed number and sequence and typically indexed...

 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
Visual Basic .NET
Visual Basic .NET , is an object-oriented computer programming language that can be viewed as an evolution of the classic Visual Basic , which is implemented on the .NET Framework...

, 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
Marshalling (computer science)
In computer science, marshalling is the process of transforming the memory representation of an object to a data format suitable for storage or transmission...

.

In unrelated usage, variant type is also used to refer to an algebraic data type
Algebraic data type
In computer programming, particularly functional programming and type theory, an algebraic data type is a datatype each of whose values is data from other datatypes wrapped in one of the constructors of the datatype. Any wrapped datum is an argument to the constructor...

 (comparable to a tagged union
Tagged union
In computer science, a tagged union, also called a variant, variant record, discriminated union, or disjoint union, is a data structure used to hold a value that could take on several different, but fixed types. Only one of the types can be in use at any one time, and a tag field explicitly...

), whose constructors are often called variants. In languages such as OCaml and Haskell
Haskell (programming language)
Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...

, 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 these two examples:

Dim A
Dim A as Variant

In C++, a variant named A can be declared as follows:

VARIANT A;

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
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.

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.
VarTypeData bytesC and C++ typeTypeName
0 Empty1
1 Null2
10 2A000A80 HRESULT (long int) Error
10 80020004 HRESULT (long int) Missing3
17 2A BYTE (unsigned char) Byte
11 FFFF VARIANT_BOOL (short int) Boolean
2 2A00 short int Integer
3 2A000000 long int Long
4 00002842 float Single
5 0000000000004540 double Double
6 A068060000000000 CY structure Currency
7 00000000C0D5E140 DATE (double) Date
8 xxxxxxxx BSTR (wchar t
Wide character
A wide character is a computer character datatype that generally has a size greater than the traditional 8-bit character. The increased datatype size allows for the use of larger coded character sets.-History:...

 pointer)
String
9 00000000 IUnknown
IUnknown
In programming, the IUnknown interface is the fundamental interface in the Component Object Model . The published mandates that COM objects must minimally implement this interface...

 pointer
Nothing4
9 xxxxxxxx IUnknown pointer Object reference5
  • 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
    Interface (computer science)
    In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...

     pointer, that is, a pointer to a pointer a Virtual method table
    Virtual method table
    A virtual method table, virtual function table, dispatch table, or vtable, is a mechanism used in a programming language to support dynamic dispatch ....

     (which is an array of function pointers).

Collections

The Collection class in OLE Automation
OLE Automation
In Microsoft Windows applications programming, OLE Automation , is an inter-process communication mechanism based on Component Object Model that was intended for use by scripting languages – originally Visual Basic – but now are used by languages run on Windows...

 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
IDispatch
IDispatch is the interface that exposes the OLE Automation protocol. It is one of the standard interfaces that can be exposed by COM objects. The I in IDispatch refers to interface...

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 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(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.

Links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK