This article needs additional citations for verification . (May 2019) (Learn how and when to remove this template message) |
Variant is a data type in certain programming languages, particularly Visual Basic, OCaml, [1] Delphi 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 (for example, integer, floating-point, single- and double-precision, object, etc.) except fixed-length string type and record 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, 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 an algebraic data type (comparable to a tagged union), whose constructors are often called variants. In languages such as OCaml and Haskell, this kind of variant type is the standard language building block for representing many data structures.
In Visual Basic, a variant named A can be explicitly declared as shown in either of these two examples:
DimADimAasVariant
In Delphi, a variant named A is declared in the following way:
varA:variant;
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:
Offset | Size | Description |
---|---|---|
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. |
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.
VarType | Hex | Propvariant Type | Propvariant Member | TypeName | Data bytes | C and C++ type |
---|---|---|---|---|---|---|
0 | 0x00 | VT_EMPTY | None | Empty1 | ||
1 | 0x01 | VT_NULL | None | Null2 | ||
2 | 0x02 | VT_I2 | iVal | Integer | 2A00 | SHORT |
3 | 0x03 | VT_I4 | lVal | Long | 2A000000 | LONG |
4 | 0x04 | VT_R4 | fltVal | Single | 00002842 | FLOAT |
5 | 0x05 | VT_R8 | dblVal | Double | 0000000000004540 | DOUBLE |
6 | 0x06 | VT_CY | cyVal | Currency | A068060000000000 | CY structure |
7 | 0x07 | VT_DATE | date | Date | 00000000C0D5E140 | DATE (double) |
8 | 0x08 | VT_BSTR | bstrVal | String | xxxxxxxx | (BSTR):(OLECHAR *):(WCHAR *):(wchar_t *) |
9 | 0x09 | VT_DISPATCH | pdispVal | |||
10 | 0x0a | VT_ERROR | scode | Error | 2A000A80 | HRESULT (long int) |
11 | 0x0b | VT_BOOL | boolVal | Boolean | FFFF | VARIANT_BOOL (short) |
12 | 0x0c | VT_VARIANT | pvarVal | Variant | VARIANT | |
13 | 0x0d | VT_UNKNOWN | punkVal | Nothing4 | 00000000 | IUnknown * |
14 | 0x0e | VT_DECIMAL | decVal | DECIMAL | ||
16 | 0x10 | VT_I1 | cVal | Byte | CHAR | |
17 | 0x11 | VT_UI1 | bVal | Byte | 2A | BYTE (unsigned char) |
18 | 0x12 | VT_UI2 | uiVal | WORD (unsigned short) | ||
19 | 0x13 | VT_UI4 | ulVal | DWORD (unsigned int) | ||
20 | 0x14 | VT_I8 | hVal | |||
21 | 0x15 | VT_UI8 | uhVal | |||
22 | 0x16 | VT_INT | intVal | |||
23 | 0x17 | VT_UINT | uintVal | |||
24 | 0x18 | VT_VOID | ||||
25 | 0x19 | VT_HRESULT | Missing3 | 80020004 | HRESULT (long int) | |
26 | 0x1a | VT_PTR | ||||
27 | 0x1b | VT_SAFEARRAY | parray | |||
28 | 0x1c | VT_CARRAY | ||||
29 | 0x1d | VT_USERDEFINED | ||||
30 | 0x1e | VT_LPSTR | pszVal | |||
31 | 0x1f | VT_LPWSTR | pwszVal | |||
36 | 0x24 | VT_RECORD | ||||
37 | 0x25 | VT_INT_PTR | pintVal | |||
38 | 0x26 | VT_UINT_PTR | puintVal | |||
8192 | 0x2000 | VT_ARRAY | parray | |||
9 | Object reference5 | xxxxxxxx | IUnknown * |
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.
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.
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.
C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, while a static type system prevents unintended operations. By design, C provides constructs that map efficiently to typical machine instructions and has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computers, from supercomputers to PLCs and embedded systems.
In computer science, a reference is a value that enables a program to indirectly access a particular datum, 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.
The syntax of the C programming language is the set of rules governing writing of software in the 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.
In computer science, a pointer is a programming language object 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, please see Comparison of the Java and .NET platforms.
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.
The syntax of the 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 computer programming, an automatic variable is a local variable which is allocated and deallocated automatically when program flow enters and leaves the variable's scope. The scope is the lexical context, particularly the function or block in which a variable is defined. Local data is typically invisible outside the function or lexical context where it is defined. Local data is also invisible and inaccessible to a called function, but is not deallocated, coming back in scope as the execution thread returns to the caller.
C++/CLI is a language specification created by Microsoft which supersedes Managed Extensions for C++. It is a complete revision that simplifies the now-deprecated Managed C++ syntax and provides interoperability with Microsoft .Net languages such as C#. C++/CLI was standardized by Ecma as ECMA-372. It is currently available in Visual Studio 2005, 2008, 2010, 2012, 2013, 2015, 2017 and 2019 including the Express editions.
C# is a general-purpose, multi-paradigm programming language encompassing strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines. It was developed around 2000 by Microsoft as part of its .NET initiative, and later approved as an international standard by Ecma (ECMA-334) and ISO. Mono is the name of the free and open-source project to develop a compiler and runtime for the language. C# is one of the programming languages designed for the Common Language Infrastructure (CLI).
Evaluation strategies are used by programming languages to determine two things—when to evaluate the arguments of a function call and what kind of value to pass to the function.
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 the 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.
Visual Basic is a third-generation event-driven programming language from Microsoft for its Component Object Model (COM) programming model first released in 1991 and declared legacy during 2008. Microsoft intended Visual Basic to be relatively easy to learn and use. Visual Basic was derived from BASIC and enables the rapid application development (RAD) of graphical user interface (GUI) applications, access to databases using Data Access Objects, Remote Data Objects, or ActiveX Data Objects, and creation of ActiveX controls and objects.
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.
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.
In computer programming, programming languages are often colloquially classified as to whether the language's type system makes it strongly typed or weakly typed.
Visual Basic .NET was released by Microsoft in 2002 as a successor to the original Visual Basic computer programming language. It was implemented on the .NET Framework 1.0. The main new feature was managed code. Programmers familiar only with Visual Basic would probably have encountered difficulties working with the new version or adapting existing programs for it.