Variants — variables without a fixed data type— were the bread and butter of VB programmers up to version 6. Variants are the opposite of strictly typed variables: They can store all types of values, from a single character to an object. If you’re starting with VB 2008, you should use strictly typed variables. However, variants are a major part of the history of VB, and most applications out there (the ones you may be called to maintain) use them. I will discuss variants briefly in this section and show you what was so good (and bad) about them.
Variants, or object variables, were the most flexible data types because they could accommodate all other types. A variable declared as Object (or a variable that hasn’t been declared at all) is handled by Visual Basic according to the variable’s current contents. If you assign an integer value to an object variable, Visual Basic treats it as an integer. If you assign a string to an object variable, Visual Basic treats it as a string. Variants can also hold different data types in the course of the same program. Visual Basic performs the necessary conversions for you.
To declare a variant, you can turn off the Strict option and use the Dim statement without specifying a type, as follows:
Dim myVar
Code language: VB.NET (vbnet)
If you don’t want to turn off the Strict option (which isn’t recommended, anyway), you can declare the variable with the Object data type:
Dim myVar As Object
Code language: VB.NET (vbnet)
Every time your code references a new variable, Visual Basic will create an object variable. For example, if the variable validKey hasn’t been declared, when Visual Basic runs into the following line, it will create a new object variable and assign the value 002-6abbgd to it:
validKey = "002-6abbgd"
Code language: VB.NET (vbnet)
You can use object variables in both numeric and string calculations. Suppose that the variable modemSpeed has been declared as Object with one of the following statements:
Dim modemSpeed ' with Option Strict = Off
Dim modemSpeed As Object ' with Option Strict = On
Code language: VB.NET (vbnet)
and later in your code you assign the following value to it:
modemSpeed = "28.8"
Code language: VB.NET (vbnet)
The modemSpeed variable is a string variable that you can use in statements such as the following:
MsgBox "We suggest a " & modemSpeed & " modem."
Code language: VB.NET (vbnet)
This statement displays the following message:
"We suggest a 28.8 modem."
Code language: VB.NET (vbnet)
You can also treat the modemSpeed variable as a numeric value with the following statement:
Debug.WriteLine "A " & modemSpeed & " modem can transfer " &
modemSpeed * 1024 / 8 & " bytes per second."
Code language: VB.NET (vbnet)
This statement displays the following message:
"A 28.8 modem can transfer 3686.4 bytes per second."
Code language: VB.NET (vbnet)
The first instance of the modemSpeed variable in the preceding statement is treated as a string because this is the variant’s type according to the assignment statement (we assigned a string to it). The second instance, however, is treated as a number (a single-precision number). Visual Basic converts it to a numeric value because it’s used in a numeric calculation.
Another example of this behavior of variants can be seen in the following statements:
Dim I As Integer, S As String
I = 10
S = "11"
Debug.WriteLine(I + S)
Debug.WriteLine(I & S)
Code language: VB.NET (vbnet)
The first WriteLine statement will display the numeric value 21, whereas the second statement will print the string 1011. The plus operator (+) tells VB to add two values. In doing so, VB must convert the two strings into numeric values and then add them. The concatenation operator (&) tells VB to concatenate the two strings.
Visual Basic knows how to handle object variables in a way that makes sense. The result may not be what you had in mind, but it certainly is dictated by common sense. If you really want to concatenate the strings 10 and 11, you should use the & operator, which would tell Visual Basic exactly what to do. Quite impressive, but for many programmers, this is a strange behavior that can lead to subtle errors — and they avoid it. It’s up to you to decide whether to use variants and how far you will go with them. Sure, you can perform tricks with variants, but you shouldn’t overuse them to the point that others can’t read your code.