Variant type

Variant type

Variant is a data type in certain programming languages, particularly Visual Basic and C++ when using the Component Object Model.

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 (e.g. 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 variants in general (and especially of not explicitly declared variants in the case of Visual Basic) 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.

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 algebraic data type, whose constructors are often called "variants".

Format

A variable of variant type, for brevity called a "variant" needs 16 bytes storage and its layout is as follows:

Examples

In Visual Basic, a variant named A can be explicitly declared as shown in these two examples:Dim ADim A as VariantIn C++, a variant named A can be declared as follows:VARIANT A;

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.
*1 The type of an uninitialized variant.
*2 The type of a NULL value in a database, i.e. not uninitialized, nor equivalent to a C++ null pointer.
*3 The actual TypeName for missing arguments is Error.
*4 The data would be a BSTR.
*5 The object type set to a null reference.
*6 TypeName will return the name of the class of the object contained. The data would be an interface pointer, i.e. a pointer to a pointer to the first in an array of function pointers.

Common uses

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 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 IfEnd FunctionSimilarly the keyword ParamArray can be used to pass all following arguments in a variant array.


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Variant — may refer to:*Z variant *Variant Chinese character *Chess variant *List of poker variants *Tetris variants *Orthographical variant *Variant symlinks *Variant magazine *Variant type, in programming languages *Volkswagen Variant, the German make s… …   Wikipedia

  • Type 79 — may refer to: * Type 69/79 Chinese tank * Type 79 100 mm naval gun * Type 79 submachine gun * Bristol Ten seater, variant Type 79 * List of World War II British naval radar, a British World War II radar * The Chinese produced version of the SVD… …   Wikipedia

  • Type 77 pistol — Type 77 Type Semi automatic pistol Place of origin  PRC …   Wikipedia

  • Type system — Type systems Type safety Inferred vs. Manifest Dynamic vs. Static Strong vs. Weak Nominal vs. Structural Dependent typing Duck typing Latent typing Linear typing Uniqueness typing …   Wikipedia

  • Type 56 assault rifle — Type 56 (top) and AKS 47 Type Assault rifle Place of& …   Wikipedia

  • Type 89 AFV — Type 89 (YW534) Type Armoured personnel carrier Place of origin  People s Republic of China …   Wikipedia

  • variant — ● variant nom masculin Biochimie Substance qui dérive d une substance originelle par mutation. ⇒VARIANT, ANTE, part. prés., adj. et subst. I. Part. prés. de varier. II. Adj., vieilli. Qui change sans cesse. Synon. changeant, variable. Humeur… …   Encyclopédie Universelle

  • Type 81 assault rifle — Type 81 Type 81 I (top) and Type 81 (bottom). Type Assault rifle Place of origin …   Wikipedia

  • Type I topoisomerase — Type I topoisomerases are divided into two structurally and mechanistically distinct topoisomerases: type IA and type IB. * Type IA topoisomerases change the linking number of a circular DNA strand by units of strictly 1. * Type IB topoisomerases …   Wikipedia

  • Type de données — Type (informatique) Pour les articles homonymes, voir Type (homonymie). En programmation un type de données, ou simplement type, définit le genre de contenu d une donnée et les opérations pouvant être effectuées sur la variable correspondante.… …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”