[Previous] [TOC] [Next]

Overview of Variants

Variants were first introduced in version 2 of Visual Basic as a flexible data type that could hold each of the simple data types. The Variant data type was extended substantially with version 4 to include Byte, Boolean, Error, Objects, and Arrays, and a little further with version 5 to include the Decimal data type. The Decimal data type was the first data type that was not available as a "first class" data type-it is available only within a Variant-and you cannot directly declare a variable as a Decimal.

In Visual Basic 6, UDTs have been added to the list, effectively completing the set. Now a Variant can be assigned any variable or constant, whatever the type.

A variety of functions convert to these subtypes and test for these subtypes. Table 4-1 shows the development of the Variant data type through the versions of Visual Basic, along with the matching functions.

Table 4-1 The Evolution of Variants

Type Visual Basic Name Visual Basic Version Convert Function Test Function
0 Empty 2 = Empty IsEmpty
1 Null 2 = Null IsNull
2 Integer 2 CInt IsNumeric*
3 Long 2 CLng IsNumeric
4 Single 2 CSng IsNumeric
5 Double 2 CDbl IsNumeric
6 Currency 2 CCur IsNumeric
7 Date 2 CVDate/CDate IsDate
8 String 2 CStr
9 Object 4 IsObject
10 Error 4 CVErr IsError
11 Boolean 4 CBool
12 Variant 4 CVar
13 Data Object 4
14 Decimal 5 CDec IsNumeric
17 Byte 4 CByte
36 UDT 6
8192 Array 4 Array IsArray
16384 ByRef Never?

*Strictly speaking, IsNumeric tests to see if a variable can be converted to a numeric value, and is not simply reporting on a Variant's subtype.

[Previous] [TOC] [Next]