Dim Statement
Language Items List
Definition:
Declares the data type of and allocates storage space for variables or arrays.
The Dim statement can be used at the beginning of each statement block.
Variables are only available within the scope they are defined in.
Syntax:
Dim variable[([subscripts])][Strictly]As[New]type
[,variable[([subscripts])][Strictly]As[New]type] ...
Part Description
A variable name.
subscripts Dimensions of an array variable. You can declare multiple dimensions using the subscripts syntax that is specified in the following text. If the subscripts are omitted a dynamic array is made, and the ReDim statement must be used to
declare the bounds of the array before use.
Strictly Qualifies a given object reference variable with the name of a parent object
in its hierarchy. This named object will be the beginning location of an object
reference search, when looking for a matching field.
As type Declares the data type of variable. The type can be Double, Currency, Integer, Long, Single, String, or an
object type.
New Creates an instance of a specific object type, such as Bitmap. New is a
reserved word and cannot be used to create variables of any of the fundamental data
types (e.g. Integer) supported by Phoenix.
The argument subscripts consists of the following syntax:
[lower To]upper[,lower To]upper]...'
Syntax Description
lower Specifies the lower limit of an array dimension. It can be a range of
subscripts; negative numbers are allowed. If the lower dimension is omitted, it
defaults to 0. For example:
Dim A(-10 To 10)
Dim B(5,5)
To Reserved word that separates lower and upper values.
upper Specifies the upper limit of an array dimension.
The following statements all perform the same function:
Dim A(10,2)
Dim A(0 To 10, 0 To 2)
Dim A(10, 0 To 2)
Details:
Use a Dim statement in a Sub or Function procedure to declare a variable’s data type or object type. For example, the following statement declares the
variable's data type as Integer.
Dim MyInt As Integer
The first example below declares an object reference variable of type Bitmap,
the second variation actually makes a new Bitmap.
Dim BitmapReference As Bitmap
Dim BitmapObject As New Bitmap
In the first example the initial value of “BitmapReference” is Nothing. The following statement would be appropriate for giving a new
value to “BitmapReference”:
BitmapReference = Image1.Picture
The second example is not an object reference, it is an actual object, it
would be illegal to try to set “BitmapObject” to another value.
To declare dynamic arrays, use the Dim statement with empty parentheses. After
you declare a dynamic array, define the number of dimensions and elements in
the array by using ReDim within a procedure.
When variables are initialized, numeric variables are initialized to 0, and
strings are initialized as zero-length strings.
Note: Dim statements must be placed at the beginning of statement blocks. The
variables that they declare are only available within the block in which they are
defined.
If a Dim statement is used to create a New object, that object is referred to
as a dynamic object. Dynamic objects are automatically destroyed as soon as the
last reference to them goes away. If no reference is established to an object
created with New, the object will disappear as soon as the scope in which it
was created is done. The removal of the object usually goes unnoticed, however,
one case in which the destruction will be apparent is in the case of a Form. For
example, the last reference to a dynamic Form object could go away while it
still displayed, in which case the form would disappear from the screen.
Variables defined with the Dim statement inside of a procedure’s body are referred to as local variables. They have a certain scope or
visibility that is determined by their location inside a procedure. Consider the
following example:
0:Sub VariableTest()
- Dim var1 As Integer
- If var1 Then
- Dim var1,var2 As Integer
- Print var1, var2
- End If
- Print var1
- End Sub
On line 1, a single integer variable is defined and it is available throughout
the scope of the procedure. On line 3, two more integers are defined, one of
which happens to have the same name as an existing variable, so it hides the
previous declaration of ‘var1’. These two variables are available through the scope of the If-Block. The
If-block starts on line 2 and ends on line 5. After the If-Block, on line 6, the
original ‘var1’ is again available. The variables defined in the If-Block (var1,var2) have “gone out of scope,” meaning they are no longer available.
In the following example, there is a For-Block with an integer variable ‘j’ declared in it. While ‘j’ is only available inside of the For-Block, it is the same ‘j’ for each iteration of the loop. That is, the value of ‘j’ doesn’t keep getting reset to 0 each time through the loop. In this case, the final
value of ‘j’ will be 20.
For I = 1 To 10
Dim j As Integer
j = j + 2
Next I
By using the “Strictly” keyword with the Dim statement, Phoenix allows you to call a method that has
been overriden. If you have overridden a particular method and want to call
both the new method and the original method, you must use the Strictly modifier.
Strictly allows you to determine where to begin a lookup on an object reference,
skipping the fields defined by objects that appear after the named object in
the inheritance chain. The following example shows how this is done. The
procedure calls the BringToTop() method as it is defined on Form1, the implementation
of the BringToTop() method has been overridden by Form1, to perform some
actions, then call the original method as it is defined on Form.
Sub test()
Form1.BringToTop()
End Sub
Form1:
Sub BringToTop()
Dim f Strictly As Form
‘ Do Own Stuff
f = Me
f.BringToTop()
End Sub
See Also:
Assignment Statements
ReDim Statement
Static Statement