Variant type (COM)

Last updated

Variant is a data type in certain programming languages, particularly Visual Basic, OCaml, [1] Delphi and C++ when using the Component Object Model. It is an implementation of the eponymous concept in computer science.

Contents

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.

Examples

In Visual Basic, a variant named A can be declared either explicitly or implicitly:

DimADimAasVariant

In Delphi, a variant named A is declared in the following way:

varA: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
02The value returned by VarType; specifies what kind of data the variant contains.
26Reserved bytes; used only for VT_DECIMAL type.
8up to 8The 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.

VarTypeHexPropvariant TypePropvariant MemberTypeNameData bytesC and C++ type
00x00VT_EMPTYNoneEmpty1
10x01VT_NULLNoneNull2
20x02VT_I2iValInteger2A00SHORT
30x03VT_I4lValLong2A000000LONG
40x04VT_R4fltValSingle00002842FLOAT
50x05VT_R8dblValDouble0000000000004540DOUBLE
60x06VT_CYcyValCurrencyA068060000000000CY structure
70x07VT_DATEdateDate00000000C0D5E140DATE (double)
80x08VT_BSTRbstrValStringxxxxxxxx(BSTR):(OLECHAR *):(WCHAR *):(wchar_t *)
90x09VT_DISPATCHpdispVal
100x0aVT_ERRORscodeError2A000A80HRESULT (long int)
110x0bVT_BOOLboolValBooleanFFFFVARIANT_BOOL (short)
120x0cVT_VARIANTpvarValVariantVARIANT
130x0dVT_UNKNOWNpunkValNothing400000000 IUnknown *
140x0eVT_DECIMALdecValDECIMAL
160x10VT_I1cValByteCHAR
170x11VT_UI1bValByte2ABYTE (unsigned char)
180x12VT_UI2uiValWORD (unsigned short)
190x13VT_UI4ulValDWORD (unsigned int)
200x14VT_I8hVal
210x15VT_UI8uhVal
220x16VT_INTintVal
230x17VT_UINTuintVal
240x18VT_VOID
250x19VT_HRESULTMissing380020004HRESULT (long int)
260x1aVT_PTR
270x1bVT_SAFEARRAYparray
280x1cVT_CARRAY
290x1dVT_USERDEFINED
300x1eVT_LPSTRpszVal
310x1fVT_LPWSTRpwszVal
360x24VT_RECORD
370x25VT_INT_PTRpintVal
380x26VT_UINT_PTRpuintVal
81920x2000VT_ARRAYparray
9Object reference5xxxxxxxxIUnknown *

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

FunctionGetText(OptionalByValIndex)AsStringIfIsMissing(Index)ThenGetText=Item(CurrentItem)ElseGetText=Item(Index)EndIfEndFunction

Similarly the keyword ParamArray can be used to pass all following arguments in a variant array.

See also

Related Research Articles

In computer programming, a reference is a value that enables a program to indirectly access a particular data, such as a variable's value or a record, in the computer's memory or in some other storage device. The reference is said to refer to the datum, and accessing the datum is called dereferencing the reference. A reference is distinct from the datum itself.

The syntax of the C programming language is the set of rules governing writing of software in the C language. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.

<span class="mw-page-title-main">Pointer (computer programming)</span> Object which stores memory addresses in a computer program

In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number and reading the text found on that page. The actual format and content of a pointer variable is dependent on the underlying computer architecture.

This article compares two programming languages: C# with Java. While the focus of this article is mainly the languages and their features, such a comparison will necessarily also consider some features of platforms and libraries. For a more detailed comparison of the platforms, see Comparison of the Java and .NET platforms.

In class-based, object-oriented programming, a constructor is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

A struct in the C programming language is a composite data type declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed via a single pointer or by the struct declared name which returns the same address. The struct data type can contain other data types so is used for mixed-data-type records such as a hard-drive directory entry, or other mixed-type records.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured programs

The syntax of Java refers to the set of rules defining how a Java program is written and interpreted.

In computing, an uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one. As such, it is a programming error and a common source of bugs in software.

In some programming languages, const is a type qualifier that indicates that the data is read-only. While this can be used to declare constants, const in the C family of languages differs from similar constructs in other languages in being part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking. In other languages, the data is not in a single memory location, but copied at compile time on each use. Languages which utilize it include C, C++, D, JavaScript, Julia, and Rust.

C++/CLI is a variant of the C++ programming language, modified for Common Language Infrastructure. It has been part of Visual Studio 2005 and later, and provides interoperability with other .NET languages such as C#. Microsoft created C++/CLI to supersede Managed Extensions for C++. In December 2005, Ecma International published C++/CLI specifications as the ECMA-372 standard.

In a programming language, an evaluation strategy is a set of rules for evaluating expressions. The term is often used to refer to the more specific notion of a parameter-passing strategy that defines the kind of value that is passed to the function for each parameter and whether to evaluate the parameters of a function call, and if so in what order. The notion of reduction strategy is distinct, although some authors conflate the two terms and the definition of each term is not widely agreed upon.

C# and Visual Basic .NET are the two primary languages used to program on the .NET Framework.

Nullable types are a feature of some programming languages which allow a value to be set to the special value NULL instead of the usual possible values of the data type. In statically typed languages, a nullable type is an option type, while in dynamically typed languages, equivalent behavior is provided by having a single null value.

This is an overview of Fortran 95 language features. Included are the additional features of TR-15581:Enhanced Data Type Facilities, which have been universally implemented. Old features that have been superseded by new ones are not described – few of those historic features are used in modern programs although most have been retained in the language to maintain backward compatibility. The current standard is Fortran 2018; many of its new features are still being implemented in compilers. The additional features of Fortran 2003, Fortran 2008 and Fortran 2018 are described by Metcalf, Reid and Cohen.

The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, including C#, PowerShell as of version 7.0.0, Perl as of version 5.10, Swift, and PHP 7.0.0. While its behavior differs between implementations, the null coalescing operator generally returns the result of its left-most operand if it exists and is not null, and otherwise returns the right-most operand. This behavior allows a default value to be defined for cases where a more specific value is not available.

Protel stands for "Procedure Oriented Type Enforcing Language". It is a programming language created by Nortel Networks and used on telecommunications switching systems such as the DMS-100. Protel-2 is the object-oriented version of Protel.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

This comparison of programming languages compares how object-oriented programming languages such as C++, Java, Smalltalk, Object Pascal, Perl, Python, and others manipulate data structures.

In computer programming, a constant is a value that should not be altered by the program during normal execution, i.e., the value is constant. When associated with an identifier, a constant is said to be "named," although the terms "constant" and "named constant" are often used interchangeably. This is contrasted with a variable, which is an identifier with a value that can be changed during normal execution, i.e., the value is variable.

The Is functions are a set of functions in Microsoft's Visual Basic 6, Visual Basic for Applications, VBScript, and Visual Basic .NET. Several of them are also provided in Transact-SQL by the .NET Framework Data Provider for Microsoft SQL Server.

References

  1. "Chapter 6. Variants / Real World OCaml". v1.realworldocaml.org. Archived from the original on 2019-05-08.