Variable Declaration in Visual Basic
You declare a variable to specify its name and characteristics. The declaration statement for variables is the Dim Statement. Its location and contents determine the variable's characteristics.
For variable naming rules and considerations, see Declared Element Names.
Local and Member Variables
A local variable is one that is declared within a procedure. A member variable is a member of a Visual Basic type; it is declared at module level, inside a class, structure, or module, but not within any procedure internal to that class, structure, or module.
Shared and Instance Variables
In a class or structure, the category of a member variable depends on whether or not it is shared. If it is declared with the Shared keyword, it is a shared variable, and it exists in a single copy shared among all instances of the class or structure.
Otherwise it is an instance variable, and a separate copy of it is created for each instance of the class or structure. A given copy of an instance variable is available only to the instance of the class or structure in which it was created. It is independent of a copy of the instance variable in any other instance of the class or structure.
Declaring Data Type
The As clause in the declaration statement allows you to define the data type or object type of the variable you are declaring. You can specify any of the following types for a variable:
An elementary data type, such as
A composite data type, such as an array or structure
An object type, or class, defined either in your application or in another application
You can declare several variables in one statement without having to repeat the data type. In the following statements, the variables
k are declared as type
Dim i, j, k As Integer ' All three variables in the preceding statement are declared as Integer. Dim l, m As Long, x, y As Single ' In the preceding statement, l and m are Long, x and y are Single.
Local Type Inference
Type inference is used to determine the data types of local variables declared without an
As clause. The compiler infers the type of the variable from the type of the initialization expression. This enables you to declare variables without explicitly stating a type. In the following example, both
num2 are strongly typed as integers.
Public Sub inferenceExample() ' Using explicit typing. Dim num1 As Integer = 3 ' Using local type inference. Dim num2 = 3 End Sub
Characteristics of Declared Variables
The lifetime of a variable is the period of time during which it is available for use. In general, a variable exists as long as the element that declares it (such as a procedure or class) continues to exist. If the variable does not need to continue existing beyond the lifetime of its containing element, you do not need to do anything special in the declaration. If the variable needs to continue to exist longer than its containing element, you can include the
Shared keyword in its
Dim statement. For more information, see Lifetime in Visual Basic.
The scope of a variable is the set of all code that can refer to it without qualifying its name. A variable's scope is determined by where it is declared. Code located in a given region can use the variables defined in that region without having to qualify their names. For more information, see Scope in Visual Basic.
A variable's access level is the extent of code that has permission to access it. This is determined by the access modifier (such as Public or Private) that you use in the
Dim statement. For more information, see Access levels in Visual Basic.