Visual Basic Variables and Formulas
Visual Basic Variables and Formulas
NET FRAMEWORK
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.
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.
For example, the following statement creates space for a variable named LastName that will hold a
textual, or string, value:
Note that in addition to identifying the variable by name, we used the As keyword to give the variable
a particular type, and I’ve identified the type by using the keyword String.
A string variable contains textual information: words, letters, symbols—even numbers. They hold
names, places, lines from a poem, the contents of a file, and many other “wordy” data.
Why do you need to declare variables? Visual Basic wants you to identify the name and the type of
your variables in advance so that the compiler can set aside the memory the program will need to
store and process the information held in the variables.
After you declare a variable, you’re free to assign information to it in your code by using the
assignment operator (=). For example, the following program statement assigns the last name
“Jefferson” to the LastName variable:
LastName = "Jefferson"
Be careful to assign a textual value to the LastName variable because its data type is String. I can also
assign values with spaces, symbols, or numbers to the variable, such as
After the LastName variable is assigned a value, it can be used in place of the name “Jefferson” in
your code. For example, the assignment statement
Label1.Text = LastName
displays