See Also Example
Used at the procedure level Variables declared with the Static statement retain their value as long as the program is running. to declare variables and allocate storage space.
Static varname[([subscripts])] [As [New] type] [, varname[([subscripts])] [As [New] type] ] . . .
The Static statement has these parts:
varname Name of a variable.
subscripts Dimensions of an array . You can declare multiple dimensions. The syntax of subscripts is described below.
As type Reserved word used to declare the data type of the 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:
Static A(0 To 8, 0 To 3)
Static 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:
Static A(-4 To 10)
Static B(-99 To -5, -3 To 0)
The maximum number of array dimensions allowed in a Static 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 the Static statement in nonstatic procedures to explicitly declare Static variables. You must use Static to declare a fixed-size array in nonstatic procedures. In Static procedures, you can use either Static or Dim to declare Static variables.
Use a Static statement within a procedure to declare the data type of a Static variable. For example, the following statement declares a fixed-size array of integers:
Static EmployeeNumber(200) As Integer
Also use the Static statement 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:
Static 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.
Note The Static statement and the Static reserved word affect the lifetime of variables differently. If you declare a procedure using the Static reserved word (as in Static Sub CountSales ()), the storage space for all local variables within the procedure is allocated once and the value of the variables is preserved for the entire time the program is running. For nonstatic procedures, storage space for variables is allocated each time the procedure is called and released when the procedure is exited. The Static statement is used to declare variables within nonstatic procedures to preserve their value as long as the program is running.
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 Static statement in a procedure, it is a generally accepted programming practice to put Static statements at the beginning of the procedure with any Dim statements.
Option Base Statement
Static Statement Example
The example uses the Static statement to declare a persistent variable in the Picture_Click procedure. 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(1 To 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 As Integer ' 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