Variant is a
data type
In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
in certain programming languages, particularly
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to:
* Visual Basic .NET (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET
* Visual Basic (c ...
,
OCaml
OCaml ( , formerly Objective Caml) is a general-purpose, multi-paradigm programming language which extends the Caml dialect of ML with object-oriented features. OCaml was created in 1996 by Xavier Leroy, Jérôme Vouillon, Damien Doligez, D ...
,
[ ] Delphi
Delphi (; ), in legend previously called Pytho (Πυθώ), in ancient times was a sacred precinct that served as the seat of Pythia, the major oracle who was consulted about important decisions throughout the ancient classical world. The oracl ...
and
C++ when using the
Component Object Model. It is an implementation of the
eponymous concept in
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includin ...
.
In Visual Basic (and
Visual Basic for Applications
Visual Basic for Applications (VBA) is an implementation of Microsoft's event-driven programming language Visual Basic 6.0 built into most desktop Microsoft Office applications. Although based on pre-.NET Visual Basic, which is no longer supporte ...
) the Variant data type is a
tagged union
In computer science, a tagged union, also called a variant, variant record, choice type, discriminated union, disjoint union, sum type or coproduct, is a data structure used to hold a value that could take on several different, but fixed, types. O ...
that can be used to represent any other data type (for example,
integer
An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign ( −1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language ...
,
floating-point
In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. For example, 12.345 can be ...
,
single
Single may refer to:
Arts, entertainment, and media
* Single (music), a song release
Songs
* "Single" (Natasha Bedingfield song), 2004
* "Single" (New Kids on the Block and Ne-Yo song), 2008
* "Single" (William Wei song), 2016
* "Single", by ...
- and
double
A double is a look-alike or doppelgänger; one person or being that resembles another.
Double, The Double or Dubble may also refer to:
Film and television
* Double (filmmaking), someone who substitutes for the credited actor of a character
* Th ...
-precision,
object
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an ai ...
, 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
Visual Basic, originally called Visual Basic .NET (VB.NET), is a multi-paradigm, object-oriented programming language, implemented on .NET, Mono, and the .NET Framework. Microsoft launched VB.NET in 2002 as the successor to its original Vi ...
, 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.
Examples
In Visual Basic, a variant named A can be declared either explicitly or implicitly:
Dim A
Dim A as Variant
In
Delphi
Delphi (; ), in legend previously called Pytho (Πυθώ), in ancient times was a sacred precinct that served as the seat of Pythia, the major oracle who was consulted about important decisions throughout the ancient classical world. The oracl ...
, 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:
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, that is, not uninitialized, nor equivalent to a C++
null pointer
In computing, a null pointer or null reference is a value saved for indicating that the pointer or reference does not refer to a valid object. Programs routinely use null pointers to represent conditions such as the end of a list of unknown lengt ...
.
*
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 or interfacing may refer to:
Academic journals
* ''Interface'' (journal), by the Electrochemical Society
* '' Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics''
* '' Int ...
pointer, that is, a pointer to a pointer to a
virtual method table
In computer programming, a virtual method table (VMT), virtual function table, virtual call table, dispatch table, vtable, or vftable is a mechanism used in a programming language to support dynamic dispatch (or run-time method binding).
Whe ...
(which is an array of function pointers).
Common uses
Collections
The
Collection
class in
OLE Automation
In Microsoft Windows applications programming, OLE Automation (later renamed to simply Automation) is an inter-process communication mechanism created by Microsoft. It is based on a subset of Component Object Model (COM) that was intended for use ...
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 is the interface that exposes the OLE Automation protocol. Extending IUnknown, it is one of the standard interfaces that can be exposed by COM objects. COM distinguishes between three interface types: ''custom'' that are VTABLE-based I ...
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(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.
See also
*
Tagged union
In computer science, a tagged union, also called a variant, variant record, choice type, discriminated union, disjoint union, sum type or coproduct, is a data structure used to hold a value that could take on several different, but fixed, types. O ...
*
Union type
In computer science, a union is a value that may have any of several representations or formats within the same position in memory; that consists of a variable that may hold such a data structure. Some programming languages support special data ...
References
External links
C++ Boost.Variant* https://msdn.microsoft.com/en-ca/library/cc237865.aspx
* https://msdn.microsoft.com/en-us/library/windows/desktop/aa380072(v=vs.85).aspx
{{DEFAULTSORT:Variant Type
BASIC programming language family
Data types