Dim Statement |
No
Dim varname[([subscripts])] [As [New] type] [, varname[([subscripts])] [As [New] type]] . . .
varname
Use: Required
Your chosen name for the variable.
subscripts
Use: Optional
Dimensions of an array variable.
New
Use: Optional
Keyword that creates an instance of an object.
type
Use: Optional
The data type of varname.
Declares and allocates storage space in memory for variables. The Dim statement is used either at the start of a procedure or the start of a module to declare a variable of a particular data type.
In addition to the Visual Basic data types listed in Chapter 3, type can be an Object, an object type, or a user-defined type. The default data type, when no type is explicitly declared, is Variant.
Variable-length strings are declared using the syntax:
Dim variablename As String
Fixed-length strings, on the other hand, are declared using the syntax:
Dim variablename As String * length
You can declare multiple variables in a single Dim statement, but each variable you declare must use a separate As type clause.
Variables have the following values when they are first initialized with the Dim statement:
Data Type | Initial Value |
---|---|
Numeric | 0 |
Variable-length string | Zero-length string ("") |
Fixed-length string | Padded with zeros |
Variant | Empty |
Object | Nothing |
To use an object variable that has not been declared using the New keyword, the Set statement must assign an object to the variable before it can be used, as the following code fragment illustrates:
Dim oMySubObj As TestObject.SubObject Set oMySubObj = oMyObj.SubObject
If, however, you use the New keyword, a new instance of the object is automatically created when you first reference the variable, as shown in the following code fragment:
Dim oMyObj As New TestObject
To declare array variables, use the following syntax:
Fixed length, single dimension
Dim arrayname(lower To upper) As type
Example: Dim myArray(1 To 10) As Integer
Fixed length, multidimension
Dim arrayname(lower To upper, lower To upper, ...)
Example: Dim MyArray(20,30) As Integer
Variable length, single or multidimension
Dim arrayname()
Example: Dim myArray() As Integer
You can declare a multidimensional array with up to 60 dimensions.
Variable-length arrays can be resized using the ReDim statement. Fixed-length arrays can't be resized.
If you don't state lower, the index of the first element of the array is either the number indicated in the Option Base statement, or zero if Option Base hasn't been used.
Public Sub Main() Dim varArr As Variant Dim intCtr As Integer varArr = MakeArray() For intCtr = 0 To UBound(varArr) Debug.Print intCtr & ": " & varArr(intCtr) Next End Sub Private Function MakeArray() As Variant Dim x As Variant x = Array(5, 6, 7, 8) MakeArray = x End Function
It's accepted practice to place all the Dim statements to be used in a particular procedure at the beginning of that procedure.
To declare a multidimensional array that can handle different types of data in each dimension, declare the array as a Variant.
When you declare an object reference as WithEvents, that object's events can be handled within your application. Object variables must be declared WithEvents at the module level to allow you to provide an error handler. The reason for this is that if you declared the object variable WithEvents inside a procedure, the object variable would have scope and lifetime only within that procedure.
When you declare an object variable as WithEvents in the declarations section of the module, the name of the object variable appears in the Object drop-down list at the top left of your code window. Select this, and note that the events exposed by the object are available in the Procedure drop-down list at the top right of the code window. You can then add code to these event procedures in the normal way, as shown below:
Private WithEvents oEmp As Employee Private Sub oEmp_CanDataChange(EmployeeCode As String, Cancel As Boolean) 'event handling code goes here End Sub Private Sub oEmp_DataChanged(EmployeeCode As String) 'event handling code goes here End Sub
For a fuller description and discussion of the uses of WithEvents, Event, and RaiseEvent, see the Event, RaiseEvent, and WithEvents entries, as well as Chapter 4.
The way in which you declare an Object variable with the Dim statement dictates whether your application uses late binding or early binding. Early binding allows object references to be resolved at compile time and objects to be initialized as your program is loaded into memory; therefore, they are available for use within your program almost instantaneously. Late binding, on the other hand, resolves an object reference and initializes an object only when it's referenced in the code at runtime; therefore, your program can appear to "hang" in mid-air while you wait for the object to be created in memory. To optimize the performance of your application, you should use early binding whenever possible. For more information, see Chapter 4.
Variables declared with Dim at the module level are available to all procedures within the module. At the procedure level, variables are available only within the procedure.
If you are calling a function that returns an array, define the array variable as a simple variant. When the function returns, it's a variant array, as the previous example illustrates.
Take care when dimensioning fixed-length strings; you can't use them to pass data to external DLLs that are expecting string arguments.