Visual Basic Variant Datatype
The Visual Basic Variant datatype can be used to define variables that contain any type of data. If a variable is declared without specifying a datatype Visual Basic considers the datatype as Variant. Take a look at these examples:
Dim var3 as VariantBoth the above statements will create variables of variant datatype. Variants can store all types of information, but some overhead is required before they can be used in a calculation.
Dim var2, var3
Visual Basic Variant data is not type-less. Think of a Variant variable as one that can change type during runtime rather than as a variable without a type. A tag is stored with the Variant data to identify the type of data that it currently contains. You can examine the tag by using the VarType function.
Among the major changes in Visual Basic .NET, is that the Variant datatype has been replaced with the .NET object type.
Initializing Variant DataWhen variables are initialized, a numeric variable is initialized to 0 (zero), variable length string is initialized to a zero-length string (""), whereas Variant variables are initialized to Empty. An Empty Variant is converted to zero when used in a numeric expression, or to an empty string when used in a string expression. You can use the IsEmpty function to check whether a variant is uninitialized (empty).
Null values can also be stored in Variants. You can test whether a variant contains a null value with the IsNull function. Null is not the same as Empty, which indicates that a variant has not yet been initialized.
Manipulating Variant DataVisual Basic coerces Variant data as needed to complete comparisons and operations. If you use the + operator on a numerical string and a number or numerical string, Visual Basic adds the two numbers. If you use the + operator on two character or alphanumeric strings, Visual Basic concatenates the values. However, if one of the strings is numerical and the other contains characters, Visual Basic tries to convert the character string into a number and add the two values. If VB cannot convert the string to a number, it returns a type mismatch error. Automatic data coercion is always dangerous because you might not get the results that you expect.
When To Use Variant DatatypeWhile the use of 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.
The visual basic variant datatype is convenient, but it's not always the best choice. It's tempting to declare all your variables as Variants so you don’t have to worry about what is in them. The explicit datatypes are available because they are useful. If you think something will always be an Integer, declare it as an Integer.
Variants are also slower than explicitly declared variables for the same operations since they have the overhead of tracking which type of data they are holding at a given time. In addition, Variants are larger than almost any other datatype and so take longer to move around in the memory.
In some instances, you have no choice about the datatype. If you are assigning values for variables that might at some point need to contain a null value, you must use the Visual Basic Variant datatype. This is the only datatype that can contain a null value, and attempting to assign a null value to a non-variant variable trigger a runtime error. The same goes for function return values. If a function might need to return a null value, the return value of the function must be a Variant.
Back to Visual Basic Help