Difference between revisions of "Declaring Visual Basic Variables and Constants"
Line 5: | Line 5: | ||
The difference between variables and constants is that the value stored in a variable can be changed at any time after it has been created. The value assigned to a constant, as the name suggests, cannot be changed once it has been declared an initialized. | The difference between variables and constants is that the value stored in a variable can be changed at any time after it has been created. The value assigned to a constant, as the name suggests, cannot be changed once it has been declared an initialized. | ||
− | == Declaring | + | == Declaring Visual Basic Variables == |
Variables are declared using the Visual Basic ''Dim'' keyword. The syntax for a simple declaration of a variable is as follows: | Variables are declared using the Visual Basic ''Dim'' keyword. The syntax for a simple declaration of a variable is as follows: | ||
'''Dim''' ''variableName'' '''As''' ''variableType'' | '''Dim''' ''variableName'' '''As''' ''variableType'' | ||
+ | |||
+ | In the above outline, ''Dim'' is the keyword which indicates to Visual Basic that a variable is being declared. ''variableName'' is the name assigned to the variable. Try to use a descriptive variable name and prefix the name with something which indicates the variable type. For example, when declaring a String variable prefix the name with ''str'' (e.g strFirstName). The ''As'' keyword precedes the declaration of the variable type (String, Date, Integer etc). For a complete list of Visual Basic variable types see [[Understanding Visual Basic Variable & Constant Types]]. | ||
+ | |||
+ | To declare an Integer value named intInterestRate for example: | ||
+ | |||
+ | <pre> | ||
+ | Dim intInterestRate As Integer | ||
+ | </pre> | ||
+ | |||
+ | It is also possible to declare multiple variables on the same line. The only prerequisite is that the variables be of the same type: | ||
+ | |||
+ | <pre> | ||
+ | Dim intInterestRate, intExchangeRate As Integer | ||
+ | </pre> |
Revision as of 15:13, 1 August 2007
The different types of Visual Basic variables and constants was described in detail in the previous chapter (Understanding Visual Basic Variable & Constant Types). In this chapter we will learn how to declare and initialize variables and constants in Visual Basic.
Before learning how to declare variables and constants it is worth a quick reminder of the difference between variables and constants in Visual Basic. Both constants and variables provide a way to reserve memory locations for the storage of values of various types (such as numbers, characters and strings). These memory locations are assigned names which can be used in Visual Basic code to reference these memory locations. For example, you might want to store the interest rate for a banking application. To do so you would declare a variable with a name of your choice (such as interestRate) and specify the variable as an Interger type (since it will be storing a number). Having declared the variable you can assign a value to it and read that value anywhere in your application code.
The difference between variables and constants is that the value stored in a variable can be changed at any time after it has been created. The value assigned to a constant, as the name suggests, cannot be changed once it has been declared an initialized.
Declaring Visual Basic Variables
Variables are declared using the Visual Basic Dim keyword. The syntax for a simple declaration of a variable is as follows:
Dim variableName As variableType
In the above outline, Dim is the keyword which indicates to Visual Basic that a variable is being declared. variableName is the name assigned to the variable. Try to use a descriptive variable name and prefix the name with something which indicates the variable type. For example, when declaring a String variable prefix the name with str (e.g strFirstName). The As keyword precedes the declaration of the variable type (String, Date, Integer etc). For a complete list of Visual Basic variable types see Understanding Visual Basic Variable & Constant Types.
To declare an Integer value named intInterestRate for example:
Dim intInterestRate As Integer
It is also possible to declare multiple variables on the same line. The only prerequisite is that the variables be of the same type:
Dim intInterestRate, intExchangeRate As Integer