Variable
A variable is a temporary storage location for data in your program. You can use one or many variables in your code, and they can contain words, numbers, dates, properties, or other values. By using variables, you can assign a short and easy-to-remember name to each piece of data you plan to work with. Variables can hold information entered by the user at run time, the result of a specific calculation, or a piece of data you want to display on your form. In short, variables are handy containers that you can use to store and track almost any type of information.
Using variables in a Visual Basic program requires some planning. Before you can use a variable, you must set aside memory in the computer for the variable’s use. This process is a little like reserving a seat at a theater or a baseball game. I’ll cover the process of making reservations for, or declaring, a variable in the next section.
The Dim Statement
To declare a variable in Visual Basic 2010, type the variable name after the Dim statement. (Dim stands for dimension.) This declaration reserves room in memory for the variable when the program runs and lets Visual Basic know what type of data it should expect to see later. Although this declaration can be done at any place in the program code (as long as the declaration happens before the variable is used), most programmers declare variables in one place at the top of their event procedures or code modules.
For example, the following statement creates space for a variable named FirstName that will hold a textual, or string, value:
Dim FirstName as String
Use the As keyword to give the variable a particular type, and I’ve identified the type by using the keyword String. (You’ll learn about other data types later in this chapter.) A string variable contains textual information: words, letters, symbols—even numbers. I find myself using string variables a lot; they hold names, places, lines from a poem, the contents of a file, and many other “wordy” data.
Implicit Variable Declaration
If you really want to declare variables “the old way” in Visual Basic 2010—that is, without explicitly declaring them by using the Dim statement—you can place the Option Explicit Off statement at the very top of your form’s or module’s program code (before any event procedures), and it will turn off the Visual Basic default requirement that variables be declared before they’re used. This method is not recommend, but you might find it useful temporarily as you convert older Visual Basic programs to Visual Basic 2010.
Another possibility is to use the Option Infer statement, which was added to Visual Basic 2008. If Option Infer is set to On, Visual Basic will deduce or infer the type of a variable by examining the initial assignment you make. This allows you to declare variables without specifically identifying the type used, and allowing Visual Basic to make the determination. For example, the expression
Dim attendance = 100
will declare the variable named attendance as an Integer, because 100 is an integer expression. In other words, with Option Infer set to On, it is the same as typing
Dim attendance As Integer = 100
Likewise, the expression
Dim address = "Bireuen"
will declare the variable address as type String, because its initial assignment was of type String. If you set Option Infer to Off, however, Visual Basic will declare the variable as type Object—a general (though somewhat bulky and inefficient) container for any type of data.
If you plan to use Option Infer to allow this type of inferred variable declaration (a flexible approach, but one that could potentially lead to unexpected results), place the following two statements at the top of your code module (above the Class Form statement):
Option Explicit Off
Option Infer On
Option Explicit Off allows variables to be declared as they are used, and Option Infer On allows Visual Basic to determine the type automatically. You can also set these options using the Options command on the Tools menu, as discussed in Chapter 1, “Exploring the Visual Studio Integrated Development Environment.”