Static Statement
Language Items List
Definition:
Used to declare variables and allocate storage space. Variables declared with
Static statement retain their value as long as the program is running.
Syntax:
Static variable[([subscripts])][Strictly]As[New]type
[,variable[([subscripts])][Strictly]As[New]type] ...
Details:
The Static statement has these parts:
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:
The Static statement can be used in nonstatic procedures to explicitly declare static variables. In Static
procedures, however, you can use either Static or Dim to declare static variables,
because all variables are automatically made as statics.
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,
strings are initialized as zero-length strings.
Note: When you use the Static statement in a procedure, it must appear as with all
Dim statements, at the beginning of the first block and can appear at the
beginning of any subsequent blocks as well.
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, which
happens to be just line 4. After the If-Block, on line 6, the original ‘var1’ is again available. The variables defined in the If-Block (var1,var2) are
said to have “gone out of scope”, they are no longer available.
Example two:
For I = 1 To 10
Dim j As Integer
j = j + 2
Next I
In the previous example, there is a For-Block with a 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.
Since statics variables are not reset after the procedure finishes, any
objects created with the New keywords as in “Static form As New Form” will have a reference to them as long as that method stays around. Compare
what happens in the following case:
Sub one()
Dim form As New Form
form.Show()
End Sub
If you use the Debugger to step through this function, you will see the object
be created and shown after the call to Show(). When the procedure exits the
object (form) will be destroyed since there are no more references to it.
Contrast this with what would happen if the Dim was changed to Static:
Sub one()
Static form As New Form
form.Show()
End Sub
Now since the variable ‘form’ remains around even after the method is done running, the instance of the
Form created in the procedure will stay around. However, if the method is edited
(recompiled), the variables of the method are essentially remade and the Form
instance will disappear if no object references have been established to it.
By using the “Strictly” keyword with the Dim statement, Phoenix allows you to call a method that has
been overridden. 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 do 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:
Dim Statement
Function Statement
ReDim Statement
Sub Statement