Static Statement

Syntax

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


varname

Use: Required

Data Type: Any

The name of the variable, following Visual Basic naming conventions.


subscripts

Use: Optional

Data Type: Long Integer

Denotes varname as an array, and specifies the number and extents of dimensions of the array.


New

Use: Optional

Type: Keyword

Used to automatically create an instance of the object referred to by the object variable varname.


type

Use: Optional

Type: Keyword

Data type of the variable varname.

Description

Used at procedure level to declare a static variable and allocate the relevant storage space in memory. Static variables retain their value between calls to the procedure in which they are declared.

Rules at a Glance

  • A static variable's scope is limited to the procedure in which it's created.

  • The subscripts argument has the following syntax:

    [lowerbound To] upperbound [, _
    [lowerbound To] upperbound]

  • Using the subscripts argument, you can declare up to 60 multiple dimensions for the array.

  • The lowerbound argument of the subscripts argument is optional. When not used, the lower bound of the array is specified by the Option Base statement. If Option Base has not been used, the lower bound of the array is zero.

  • If the subscripts argument isn't used (i.e., the variable name is followed by empty parentheses), the array is declared as dynamic. You can change both the number of dimensions and number of elements of a dynamic array by using the ReDim statement.

  • The New keyword denotes that a new instance of the object is created when the first reference to the object is made. Use of the New keyword in the Static statement therefore eliminates the need to use the Set statement.

  • You can't use the New keyword to declare variables of any intrinsic data type or instances of dependent objects.

  • If you don't use the New keyword with an object variable, you must use the Set statement to assign an existing object to the variable before you can use the variable.

  • datatype may be Byte, Boolean, Currency, Date, Double, Integer, Long, Object, Single, String, Variant, a user-defined type, or an object type.

  • If you don't specify datatype and you haven't used a DefType statement, the variable is cast as a variant.

  • The following table shows the values held by each data type when a variable is first initialized:

    Data Type Initial Value
    Numeric 0
    Variable-length string Zero-length string ("")
    Fixed-length string Filled with zeros
    Variant Empty
    Object Nothing
    Date Saturday 30 December 1899 12:00:00

  • The individual elements of a user-defined type are initialized with the value corresponding to their data types.

  • To declare a fixed length string, use the syntax:

    Static varname As String * stringlength

Programming Tips and Gotchas

  • It's a recognized programming practice when using the Static statement in a procedure to put the Static statements at the beginning of that procedure.

  • Although their value persists between calls to a procedure, static variables don't have scope outside of the procedure in which they are created.

  • You should note that when you use the New keyword, the Initialize event of the underlying object is fired on the first reference to the object, not when the object variable is declared.

  • It's good programming practice to always use Option Explicit at the beginning of a module to require variable declaration, and thus prevent misnamed variables causing hard-to-find errors.

See Also

Friend Statement, Public Statement, ReDim Statement, Set Statement
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.202.123