See Also Example
Declares variables and allocates storage space. When used in the Declarations section of a form or module, the variables declared with Dim are available to all procedures within the form or module. When used at the procedure level , the variables are available only in the procedure.
Dim varname[([subscripts])][As [New] type][, varname[([subscripts])][As [New] type] ] . . .
The Dim statement has these parts:
varname Name of a variable.
subscripts Dimensions of an array variable. You can declare multiple dimensions. The syntax of subscripts is described below.
As type Reserved word used to declare the data type of a variable. The type may be Integer, Long, Single, Double, Currency, String (for variable-length strings), String * length (for fixed-length strings), Variant , a user-defined type, or an object type . Use a separate As type clause for each variable being defined.
New Creates a new instance of a specific object type, such as Form1. The New reserved word cannot be used to create new variables of any of the fundamental data types, nor can it be used to create a variable of any generic object type (MDIForm, Form or Control.) or any specific control type (CommandButton, TextBox, and so on).
The argument subscripts has the following syntax:
[lower To ]upper[,[lower To] upper] . . .
The To reserved word provides a way to indicate both the lower and upper bounds of an array variable's subscripts. The following statements are equivalent if there is no Option Base statement:
Dim A(0 To 8, 0 To 3)
Dim A(8, 0 To 3)
Array subscripts can be negative. To can be used to specify any range of subscripts between -32,768 and 32,767, inclusive. For example:
Dim A(-4 To 10)
Dim B(-99 To -5, -3 To 0)
The maximum number of array dimensions allowed in a Dim statement is 60.
If you use a subscript that is greater than the specified maximum or smaller than the specified minimum, an error occurs.
Use a Dim statement in the Declarations section or in Sub or Function procedures to declare the data type of a variable. For example, the following statement declares the variable as an Integer.
Dim NumberOfEmployees As Integer
Also use a Dim statement in the Declarations section or in Sub or Function procedures to declare the object type of a variable. The following statement declares a variable for a new instance of a form.
Dim X As New Form1
If New is not used when declaring an object variable, no instance of the object actually exists. The variable must be assigned to an existing object of type using the Set statement before it can be used. Until it is assigned to an existing object, the declared object variable has the special value Nothing, which indicates that it does not refer to any particular instance of an object.
You can also use the Dim statement with empty parentheses to declare dynamic arrays. After declaring a dynamic array, use the ReDim statement within a procedure to define the number of dimensions and elements in the array. If you try to redeclare a dimension for an array variable whose size was explicitly specified in a Global or Dim statement, an error occurs.
When variables are initialized, numeric variables are initialized to 0, Variant variables to Empty Variable-length strings are initialized as zero-length strings, and fixed-length strings are filled with zeros. The fields of user-defined type variables are initialized as if they were separate variables..
Tip When you use the Dim statement in a procedure, it is a generally accepted programming practice to put Dim statements at the beginning of the procedure.
The reserved word Shared is preserved for compatibility with earlier versions of Basic, but has no effect in Visual Basic.
Option Base Statement
Dim Statement Example
The example uses the Dim statement to declare variables at module and procedure level. Dim is also used to declare a new instance of a form. To try this example, which also illustrates the action of the Arrange method, paste the code into the Declarations section of an MDI Form named MDIForm1 that has an MDI child form (named Form1), and a picture box (named Picture1). Then press F5 and click anywhere in the picture box.
Const FORMCOUNT = 5
Dim F(FORMCOUNT) As New Form1
Sub MDIForm_Load ()
Dim I ' Declare local variable.
Load Form1 ' Load original Form1.
For I = 1 To FORMCOUNT
F(I).Caption = "Form" & I + 1 ' Change caption on copies.
F(I).Show ' Load 5 copies of Form1.
Sub Picture1_Click ()
Static ClickCount ' Declare variables.
Dim I, PrevWidth, Start
ClickCount = ClickCount + 1 ' Increment click counter.
Select Case ClickCount
MDIForm1.Arrange 1 ' Tile horizontally.
MDIForm1.Arrange 2 ' Tile vertically.
Case 3 ' Minimize each form.
PrevWidth = MDIForm1.Width ' Get MDIForm width.
MDIForm1.Width = PrevWidth / 2 ' Divide it in half.
Form1.WindowState = 1 ' Minimize the original.
For I = 1 To FORMCOUNT ' Look at each instance of F.
If TypeOf F(I) Is Form1 Then
F(I).WindowState = 1 ' Minimize each copy.
Start = Timer ' Get timer start point.
Loop Until Timer >= Start + 5
MDIForm1.Width = PrevWidth ' Resize to original size.
MDIForm1.Arrange 3 ' Arrange icons.