encyclopegia.org

Variant type (COM)

Variant is a data type in certain programming languages, particularly Visual Basic, OCaml, Delphi and C++ when using the Component Object Model.[src] It is an implementation of the eponymous concept in computer science.[src] 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.[src] In Visual Basic, any variable not declared explicitly or the type of which is not declared explicitly, is taken to be a variant.[src] 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.[src] In fact, languages with a dynamic type system often have variant as the only available type for variables.[src] Among the major changes in Visual Basic .NET, being a .NET language, the variant type was replaced with the .NET object type.[src] There are similarities in concept, but also major differences, and no direct conversions exist between these two types.[src] 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.[src]

Variant is a data type in certain programming languages, particularly Visual Basic, OCaml, Delphi and C++ when using the Component Object Model.[src] It is an implementation of the eponymous concept in computer science.[src] 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.[src] In Visual Basic, any variable not declared explicitly or the type of which is not declared explicitly, is taken to be a variant.[src] 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.[src] In fact, languages with a dynamic type system often have variant as the only available type for variables.[src] Among the major changes in Visual Basic .NET, being a .NET language, the variant type was replaced with the .NET object type.[src] There are similarities in concept, but also major differences, and no direct conversions exist between these two types.[src] 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.[src] ==Examples== 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; ===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.[src] In other languages other kinds of variants can be used as well.[src] 1 The type of an uninitialized variant.[src] 2 The type of a NULL value in a database, that is, not uninitialized, nor equivalent to a C++ null pointer.[src] 3 Missing arguments are actually a particular Error value titled "parameter not found".[src] 4 The object type set to a null reference.[src] 5 TypeName will return the name of the class of the object contained.[src] 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).[src] ==Common uses== ===Collections=== The Collection class in OLE Automation can store items of different data types.[src] 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.[src] If in Visual Basic the For Each construct is used, the iterator variable must be of object type, or a variant.[src] ===Dispatch method calls=== In OLE Automation the IDispatch interface is used when the class of an object cannot be known in advance.[src] Hence when calling a method on such an object the types of the arguments and the return value is not known at compile time.[src] The arguments are passed as an array of variants and when the call completes a variant is returned.[src] ===Optional parameters=== In Visual Basic a procedure argument can be declared to be optional by prefixing it with the Optional keyword.[src] 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.[src] Since the value could either be a supplied value or a special value, a variant must be used.[src] 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.[src] ==See also== Tagged union Union type ==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[src]

37 sentences · 37 cited inline · 0 references stored

References

No reference rows published for this article.

Cite this article

MLA

"Variant type (COM)." encyclopegia.org, 2025, https://encyclopegia.org/wiki/variant-type-com. Accessed 26 November 2026.

Chicago

"Variant type (COM)." encyclopegia.org. Accessed November 26, 2026. https://encyclopegia.org/wiki/variant-type-com.

APA

encyclopegia.org. (2025). Variant type (COM). Retrieved November 26, 2026, from https://encyclopegia.org/wiki/variant-type-com

Accessed November 26, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

blood type

stub · references available

Type 69 tank

stub · references available

solar-type star

stub · references available

Adobe Type

stub · references available

Audi Type B

stub · references available

Audi Type G

stub · references available

Audi Type M

stub · references available

Audi Type SS

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1300930243 · 0.5 · snapshot enwiki-001300930243

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.