Used at module, class, structure, procedure, or block level to declare and allocate storage space for variables.
[ <attrlist> ] [{ Public | Protected | Friend | Protected Friend | Private | Static }] [ Shared ] [ Shadows ] [ ReadOnly ] Dim [ WithEvents ] name[ (boundlist) ] [ As [ New ] type ] [ = initexpr ]
You can use Public only at module, namespace, or file level. This means you can declare public variables in a source file or inside a module, class, or structure, but not inside a procedure. If you specify Public, you can optionally omit the Dim keyword.
You can use Protected only at class level. This means you can declare protected variables inside a class but not inside a procedure, and not at module, namespace, or file level. You can use Protected only to declare members of the class. If you specify Protected, you can optionally omit the Dim keyword.
You can use Friend only at module, namespace, or file level. This means you can declare friend variables in a source file or inside a module, class, or structure, but not inside a procedure. If you specify Friend, you can optionally omit the Dim keyword.
You can use Protected Friend only at class level. This means you can declare protected friend variables inside a class but not inside a procedure, and not at module, namespace, or file level. You can use Protected Friend only to declare members of the class. If you specify Protected Friend, you can optionally omit the Dim keyword.
You can use Private only at module level. This means you can declare private variables inside a module, class, or structure, but not at namespace or file level and not inside a procedure. If you specify Private, you can optionally omit the Dim keyword.
You can use Static only at procedure level. This means you can declare static variables inside a procedure or a block within a procedure, but not at class or module level. If you specify Static, you can optionally omit the Dim keyword.
You cannot specify Static together with either Shared or Shadows in the same variable declaration.
You can use Shared only at module, namespace, or file level. This means you can declare shared variables in a source file or inside a module, class, or structure, but not inside a procedure. If you specify Shared, you can optionally omit the Dim keyword.
You cannot specify both Static and Shared in the same variable declaration.
You can use Shadows only at module, namespace, or file level. This means you can declare shadowing variables in a source file or inside a module, class, or structure, but not inside a procedure. If you specify Shadows, you can optionally omit the Dim keyword.
You cannot specify both Static and Shadows in the same variable declaration.
You can use ReadOnly only at module, namespace, or file level. This means you can declare read-only variables in a source file or inside a module, class, or structure, but not inside a procedure. If you specify ReadOnly, you can optionally omit the Dim keyword.
If you use the WithEvents keyword, you cannot declare name as Object. You must declare it as the specific class that can raise the events.
You can declare several variables to be of the same data type. You can also specify different types for different variables or groups of variables. Each variable takes the data type specified in the first As clause encountered after its name part.
Each value in boundlist specifies the upper bound of a dimension, not the length. The lower bound is always zero, so the subscript for each dimension can vary from zero through the upper-bound value.
It is possible to use -1 to declare the upper bound of an array dimension. This signifies that the array is empty but not Nothing, a distinction required by certain common language runtime functions. However, Visual Basic code cannot successfully access such an array. If you attempt to do so, an IndexOutOfRangeException error occurs during execution.
If you do not specify type, the variable takes the data type of initexpr. If you do specify type, the data type of initexpr must be convertible to type. If neither type nor initexpr is present, the data type defaults to Object.
Each attribute in the attrlist part has the following syntax and parts:
attrname [({ attrargs | attrinit })]
Variables declared with Dim are available to all code within the region containing the Dim statement. If they are declared in a module, class, or structure, but outside any procedure, they can be accessed from anywhere within that module, class, or structure. If they are declared inside a procedure or a block, they are accessible only from within that procedure or block. To specify their accessibility in more detail, include the Public, Protected, Friend, Protected Friend, Private, or Static keywords.
The Dim statement can declare the data type of a variable and initialize its contents. The declaration statements in the following example declare an Integer variable, a Boolean variable, and an object variable. These are initialized, respectively, to 10, True, and a newly created instance of the Label class.
Dim Quantity As Integer = 10 Private FirstTry As Boolean = True Protected MyLabel As New Label
If you do not specify an initialization value for a variable, Visual Basic initializes it to the default value for its data type. The default initialization values are as follows:
Each element of a structure or array is initialized as if it were a separate variable.
You can initialize the values of an array by surrounding the initialization values with braces ({}). The following example creates an array with four elements:
Friend A() As Integer = {0, 1, 2, 3}
For multidimensional arrays, each separate dimension is surrounded with braces. The elements are specified in row-major order. The following example illustrates this:
' Initialize each array element to its row index + its column index. Public A( , ) As Integer = {{0+0, 0+1, 0+2}, {1+0 ,1+1, 1+2}}
Note You should put all your declaration statements at the beginning of the code region in which they appear, for example a module or a procedure. If a declaration statement initializes the value of a variable, it should be executed before any other statement makes reference to the variable.
A public variable can be either early bound or late bound. Protected, friend, and private variables cannot be late bound. This means you cannot access members on a late-bound object variable unless it is declared Public. If you attempt to do this, the compiler does not generate an error, but a MissingMemberException error occurs at run time.
A static variable has a longer lifetime than that of the procedure in which it is declared. The boundaries of the variable's lifetime depend on where the procedure is declared and whether or not it is Shared.
This example uses the Dim statement to declare variables and arrays. The lower bound for array subscripts is always 0, and the upper bound is the value that appears in the Dim statement.
' Declarations do not have to initialize their variables. ' AnyValue and MyValue are initialized by default to Nothing.Dim
AnyValue,
MyValueAs
Object ' Number is initialized by default to 0.Dim
NumberAs
Integer ' Multiple declarations can be of the same data type or different types.Dim
FirstNumber, SecondNumber, ThirdNumberAs
IntegerDim
MyDateAs
Date, MyValueAs
Single, MySwitchAs
Boolean ' DayArray is an array of 51 Objects indexed from 0 through 50.Dim
DayArray(
50)
As
Object ' Matrix2 is a two-dimensional array of type Integer.Dim
Matrix2(
3,
4)
As
Integer ' Matrix3 is a three-dimensional array of type Double.Dim
Matrix3(
5,
9,
5)
As
Double ' BirthDay is an array of 11 dates indexed from 0 through 10.Dim
BirthDay(
10)
As
Date ' MyArray is an array of objects. The length of the array is assigned ' when another statement assigns an array object to MyArray.Dim
MyArray()
As
Object
Const Statement | ReDim Statement |