Static Statement

Declare static variables and allocate storage space. Variables declared with the Static statement retain their values as long as the code is running.

Syntax

Static varname[([subscripts])] [As [New] type] [, varname[([subscripts])] [As [New] type]] . . .

The Static statement syntax has these parts:

Part Description
varname Required. Name of the variable; follows standard variable naming conventions.
subscripts Optional. Dimensions of an array variable; up to 10 multiple dimensions may be declared. The subscripts argument uses the following syntax:

count1[, count2] . . .

where count1, count2 are constants, indicating the upper limit of allowable indices for the defined array. The lower limit of allowable indices always equals 0. So, for a one-dimensional array the number of elements can be calculated as count1+1 .
New Optional. Keyword that enables implicit creation of an object. If you use New when declaring the object variable, a new instance of the object is created during declaration, so you don't have to use the Set statement to assign the object reference. The New keyword can't be used to declare variables of any intrinsic data type, can't be used to declare instances of dependent objects or objects that don't have built-in constructor.
type Optional. Data type of the variable; may be Byte, Boolean, Integer, Long, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, or an object type. Use a separate As type clause for each variable you declare.

Remarks

Once module code is running, variables declared with the Static statement retain their value until the module is reset or restarted. Use the Static statement in procedures to explicitly declare variables that are visible only within the procedure, but whose lifetime is the same as the module in which the procedure is defined.

Use a Static statement within a procedure to declare the data type of a variable that retains its value between procedure calls. For example, the following statement declares a fixed-size array of integers:

Static EmployeeNumber(200) As Integer

The following statement declares a variable for a new instance of a database engine:

Static Eng As New dbEngine

If the New keyword isn't used when declaring an object variable, the variable that refers to the object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it doesn't refer to any particular instance of an object. When you use the New keyword in the declaration, an instance of the object will be created.

If you don't specify a data type or object type, the variable is Variant by default.

All declared variables except objects declared with New, take the Empty value, which indicates that they are not initialized.

    Tip: It's recommended to place all delcarations in the beginning of a module or a procedure. This shortens the time of compilation.

Example

This example uses the Static statement to retain the value of a variable for as long as module code is running.

' Function definition.
Function KeepTotal(Number As Long) As Long

    ' Variable Accumulate preserves its value between calls.
    Static Accumulate As Long

    Accumulate = Accumulate + Number
    KeepTotal = Accumulate

End Function

 

See Also

Data Type Summary, Dim Statement, ReDim Statement, Set Statement, Const Statement, Function Statement, Sub Statement