This statement declares a variable for use in a class or procedure.
[Public | Private | Shared] Dim variable_name [, variable_name …] As [New] type [ = [New] init ]
-or-
[Public | Private | Shared] Dim variable_name [, variable_name …] As [New] type [ = [New] init ], variable_name [, variable_name …] As [New] type [ = [New] init ], …
Prerequisites
Parameters
variable_name
The name of the variable to be declared. In addition to the name, this field may include an array specification of the form: variable_name(dim_1 [, dim_2 …]), where dim_1 through dim_4 may be blank or contain an Integer constant defining the maximum index of the corresponding array dimension. GPL allows up to four dimensions.
type
The type to be assigned to this variable. The type may be a primitive type, the name of a built-in class, or the name of a user-defined class. The primitive type keywords are:
Boolean, Byte, Double, Integer, Short, Single
If a class name is specified, the variable becomes an object variable.
init
An expression that specifies the initial value for the new variable. It does not need to be a constant.
Remarks
If the Public or Private keywords are present, the Dim keyword may be omitted. If the Shared keyword is specified, only a single copy of this variable is created. It exists for all threads and persists even after the procedure in which is was defined has exited. All variables declared at the module level are implicitly shared, even though the Shared keyword is not allowed. Shared variables within a procedure can only be accessed from within that procedure, but their values persist and may be accessed by a subsequent procedure call.
If the Shared keyword is not specified on a Dim statement within a procedure, the variable exists only within that procedure, and it is initialized each time the procedure runs. If the Shared keyword is not specified on a Dim statement within a class definition, a separate copy of the variable exists in each object of that class type. If more than one variable_name field is specified, no init clause may be specified.
The New clause can only be specified for objects. If a New keyword is specified immediately following the As keyword, no initializer value may be specified. If no init clause is specified, the default value for numeric variables is zero, and for object variables is Nothing. If an init clause is specified for a Shared variable, the initialization takes place once when the main thread begins execution. If an init clause is specified for a non Shared variable, the initialization takes place each time the defining procedure is executed, or each time a new object of the class is created.
Examples
Dim ii As Integer
Dim ii As Integer = 10
Public ii As Integer = 10
Shared Dim count As Integer
Dim ii, jj As Integer, x As Double
Dim ii As Integer = 10, x As Double = 2.5
Dim start As Location
Dim start As New Location
See Also