Variables and Constants in Visual Basic

A program variable is a data item held in computer memory that holds the value currently assigned to it. Each variable has a name, an address in memory, and a size (usually measured in bytes) that determines the amount of memory it occupies. The variable's size is dependent on its data type. Before you can use a variable, you must declare it. An initial value is often assigned to a variable when it is declared, but this value may change many times during program execution.

To declare a variable, you use the Dim statement (you can also use a variable without first having declared it, although this is discouraged). The Dim statement tells the system what data type the variable will be, and reserves space in memory for the variable accordingly. Variables can be declared at any point in a program (as long as this occurs before the variable is actually used), although it is customary to declare them at the beginning of a procedure or code module.

The following statements declare a string variable called "lastName", and an unsigned short integer variable called "age":

Dim lastName As String
Dim age As UShort

A string variable contains alpha-numeric and other printable characters. String variables are commonly used to hold text information. Once you have declared a variable, you can assign a value to it using the assignment operator. In the following program statement, the value "Smith" is assigned to the lastName variable, and the numeric value 27 is assigned to the age variable:

lastName = "Smith"
age = 27

Once a string variable has been assigned a value, it can be used in place of a string literal in program statements. For example, The following statement displays the text "Smith" in a label named Label1:

Label1.Text = lastName

Variable scope

If a Dim statement appears within in an event procedure, the variable is visible only within the event procedure, and is known as a local variable. If the Dim statement appears in the General section of a form or module, all procedures in that form or module can access the variable, which is said to be a global variable. If you replace Dim with Public in the General section of a module, the variable is available to all forms and modules throughout the project.

User-defined data types

You can create your own data types, using the Structure statement, to group together related data items with different data types. Once you have created a new data type, you can create variables of this type using the Dim statement, in the same way you would create any other type of variable (note that a Structure statement cannot be placed inside an event procedure - it must appear at the top of a form or in a code module).

The following declaration creates a user-defined data type called "Student" that can store the first and last names, date of birth, and student ID code associated with a particular student:

Structure Student
  Dim lastName As String
  Dim firstName As String
  Dim dateOfBirth As Date
  Dim studentID As String
End Structure

The following code fragment uses the Student type. The first statement creates a variable named "computingStudent" of type Student, the second statement assigns the name "Smith" to the structure's lastName member variable, and the third statement assigns the name "John" to the structure's firstName member variable.

Dim computingStudent As Student
computingStudent.lastName = "Smith"
computingStudent.firstName = "John"


If you use a value in a program that never changes (for example a mathematical constant such as the speed of light, C) you should consider declaring that value as a constant rather than as a variable. The main difference between a constant and a variable is that, once you have declared a constant, you cannot change its value during program execution.

A constant has a meaningful name that replaces the numeric or string value that it represents in your code, increasing the readability of your program. Another advantage of using a constant is that, should you need to adjust the value of the constant for any reason, you only have to change the value specified in the constant's declaration.

Constants are declared using the Const keyword, as shown here:

Const Pi As Double = 3.141592653589

This statement creates a constant named "Pi" that can be used in place of the floating point value 3.141592653589 in other program statements.

To make a constant available to all objects and event procedures in a form, place the constant declaration at the top of the form's code. To make it available to all forms and modules in a project, place the constant declaration within a code module, and prefix it with the Public keyword as follows:

Public Const Pi As Double = 3.141592653589