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.
Declaration Levels
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
Boolean
,Long
, orDecimal
A composite data type, such as an array or structure
An object type, or class, defined either in your application or in another application
An interface type, such as IComparable or IDisposable
You can declare several variables in one statement without having to repeat the data type. In the following statements, the variables i
, j
, and k
are declared as type Integer
, l
and m
as Long
, and x
and y
as Single
:
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.
For more information on data types, see Data Types. For more information on objects, see Objects and Classes and Programming with Components.
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 num1
and 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
If you want to use local type inference, Option Infer
must be set to On
. For more information, see Local Type Inference and Option Infer Statement.
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 Static
or 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.