The Visual Basic compiler provides three options that determine how it handles variables:
- The Explicit option indicates whether you will declare all variables.
- The Strict option indicates whether all variables will be of a specific type.
- The Infer option indicates whether the compiler should determine the type of a variable from its value.
These options have a profound effect on the way you declare and use variables, and you should understand what they do. By exploring these settings, you will also understand a little better how the compiler handles variables. It’s recommended that you turn on all three of them, but old VB developers may not follow this advice.
VB 2008 doesn’t require that you declare your variables, but the default behavior is to throw an exception if you attempt to use a variable that hasn’t been previously declared. If an undeclared variable’s name appears in your code, the editor will underline the variable’s name with a wiggly line, indicating that it caught an error. The description of the error will appear in the Task List below the code window. If you rest the pointer over the segment of the statement in question, you will see the description of the error in a ToolTip box.
To change the default behavior, you must insert the following statement at the beginning of the file:
Option Explicit OffCode language: VB.NET (vbnet)
The Option Explicit statement must appear at the very beginning of the file. This setting affects the code in the current module, not in all files of your project or solution. You can turn on the Strict (as well as the Explicit) option for an entire solution. Open the solution’s properties dialog box (right-click the solution’s name in Solution Explorer and select Properties), select the Compile tab, and set the Strict and Explicit options accordingly, as shown in Figure 2.1.
Figure 2.1 – Setting the variable-related options on the project’s Properties pages
You can also set default values for the Explicit option (as well as for Strict and Infer) for all projects through the Options dialog box of the IDE. To open this dialog box, choose the Options command from the Tools menu. When the dialog box appears, select the VB Defaults tab under Projects And Solutions, as shown in Figure 2.2. Here you can set the default values for all four options. You can still change the default values for specific projects through the project’s Properties pages.
Figure 2.2 – Setting the variable-related options in the Visual Studio Options dialog box
The way undeclared variables are handled by VB 2008 is determined by the Explicit and Strict options, which can be either on or off. The Explicit option requires that all variables used in the code are declared before they’re used. The Strict option requires that variables are declared with a specific type. In other words, the Strict option disallows the use of generic variables that can store any data type.
The default value of the Explicit statement is On. This is also the recommended value, and you should not make a habit of changing this setting. In the section “Reasons for Decalring Variables” later in this chapter, you will see an example of the pitfalls you’ll avoid by declaring your variables. By setting the Explicit option to Off, you’re telling VB that you intend to use variables without declaring them. As a consequence, VB can’t make any assumption about the variable’s type, so it uses a generic type of variable that can hold any type of information. These variables are called Object variables, and they’re equivalent to the old variants.
While the option Explicit is set to Off, every time Visual Basic runs into an undeclared variable name, it creates a new variable on the spot and uses it. The new variable’s type is Object, the generic data type that can accommodate all other data types. Using a new variable in your code is equivalent to declaring it without type. Visual Basic adjusts its type according to the value you assign to it. Create two variables, var1 and var2, by referencing them in your code with statements like the following ones:
var1 = "Thank you for using Fabulous Software" var2 = 49.99Code language: VB.NET (vbnet)
The var1 variable is a string variable, and var2 is a numeric one. You can verify this with the GetType method, which returns a variable’s type. The following statements print the highlighted types shown below each statement:
Debug.WriteLine "Variable var1 is " & var1.GetType().ToString Variable var1 is System.String Debug.WriteLine "Variable var2 is " & var2.GetType().ToString Variable var2 is System.Double Later in the same program, you can reverse the assignments: var1 = 49.99 var2 = "Thank you for using Fabulous Software"Code language: VB.NET (vbnet)
If you execute the preceding statements again, you’ll see that the types of the variables have changed. The var1 variable is now a Double, and var2 is a String. The type of a generic variable is determined by the variable’s contents and it can change in the course of the application. Of course, changing a variable’s type at runtime doesn’t come without a performance penalty (a small one, but nevertheless some additional statements must be executed).
Another related option is the Strict option, which is off by default. The Strict option tells the compiler whether the variables should be strictly typed. A strictly typed variable must be declared with a specific type and it can accept values of the same type only. With the Strict option set to Off, you can use a string variable that holds a number in a numeric calculation:
Dim a As String = "25000" Debug.WriteLine a / 2 The last statement will print the value 12500 in the Immediate window. Likewise, you can use numeric variables in string calculations: Dim a As Double = 31.03 a = a + "1"Code language: VB.NET (vbnet)
If you turn the Strict option on by inserting the following statement at the beginning of the file, you won’t be able to mix and match variable types:
Option Strict OnCode language: VB.NET (vbnet)
If you attempt to execute any of the last two code segments while the Strict option is on, the compiler will underline a segment of the statement to indicate an error. If you rest the pointer over the underlined segment of the code, the following error message will appear in a tip box:
Option strict disallows implicit conversions from String to DoubleCode language: VB.NET (vbnet)
(or whatever type of conversion is implied by the statement).
When the Strict option is set to On, the compiler doesn’t disallow all implicit conversions between data types. For example, it will allow you to assign the value of an integer to a Long, but not the opposite. The Long value might exceed the range of values that can be represented by an Integer variable. You will find more information on implicit conversions in the section titled “Widening and Narrowing Conversions in VB.NET 2008,” later in this chapter.