Revision [17001]

This is an old revision of KeyPgStatic made by FxMwikki on 2014-01-18 13:35:33.



Defines variables, objects and arrays having static storage

Static symbol [ (array-dimensions) ] KeyPgAs As DataType DataType [, ...]


KeyPgType Type typename
Static symbol [ (array-dimensions) ] KeyPgAs As DataType DataType [, ...]
KeyPgEndblock End KeyPgType Type
KeyPgDim Dim typename.symbol [ (array-dimensions) ] KeyPgAs As DataType DataType [= initial-value] [, ...]

variable or array symbol name.
lower-bound KeyPgTo To upper-bound [, ...]

Specifies ProPgStorageClasses static storage for variables, objects and arrays; they are allocated at program startup and deallocated upon exit. Objects are constructed once when they are defined, and destructed upon program exit.
When declaring static arrays, only ProPgLiterals numeric literals, KeyPgConst constants or KeyPgEnum enumerations may be used as subscript range values. Static variable-length arrays must be declared empty (no subscript range list) and resized using KeyPgRedim Redim before used.
In both iterative and recursive blocks, like looping CatPgControlFlow control flow statements or procedures, static variables, objects and arrays local to the block are guaranteed to occupy the same storage across all instantiations of the block. For example, procedures that call themselves - either directly or indirectly - share the same instances of their local static variables.
A static variable may only be initialised with a constant value: its starting value is set at the start of the program before any code is run, and so it cannot depend on any variables or functions in it.
When used with module-level and member procedure declarations, Static specifies ProPgStorageClasses static storage for all local variables, objects and arrays.
At module-level variable declaration only, the modifier KeyPgShared Shared may be used with the keyword Static to make module-level static variables visible inside procedures.
When used with UDT, each Static member variable needs an explicit definition as well as a declaration. The declaration inside the UDT is the prototype that is visible to every module seeing the UDT declaration (like with methods). The definition - which must be done outside the type declaration code, and appear only once in a single module - allocates static space for - and optionally initializes - the static variable.
Such a Static member variable is created and initialized only once independently of any object construction, in contrast to non-static member variables which are created again and again, for each separate object.
A Static member variable is subject to member access control except for its definition (outside the UDT). If a private Static member variable is to be explicitly initialized outside the UDT member procedures, an initializer must be provided with the definition.
A (public) Static member variable is visible throughout the entire program (inside any procedure) even if the modifier KeyPgShared Shared is not specified in the definition (Static and KeyPgShared Shared are useless in the definition, but this may improve code readability). So, (public) Static member variables can be called directly anywhere in code, or on objects of type typename.
As a Static member variable is like a global variable declared in the UDT namespace, all variable kinds are authorized, even including variable-length arrays (only case where a data is Static as UDT member, and is dynamic as variable).
KeyPgStaticMember Static (Member) is also used in member procedure declarations to specify static member procedures.

Sub f
    '' static variables are initialized to 0 by default
    Static i As Integer
    i += 1
    Print "Number of times called: " & i
End Sub

'' the static variable in f() retains its value between
'' multiple procedure calls.
Will output:

Number of times called: 1
Number of times called: 2

'Assign an unique ID to every instance of a Type (ID incremented in order of creation)

Type UDT
    Declare Property getID () As Integer
    Declare Constructor ()
    Dim As Integer ID
    Static As Integer countID
End Type
Dim As Integer UDT.countID = 0

Property UDT.getID () As Integer
  Property = This.ID
End Property

Constructor UDT ()
  This.ID = UDT.countID
  UDT.countID += 1
End Constructor

Dim As UDT uFirst
Dim As UDT uSecond
Dim As UDT uThird

Print uFirst.getID
Print uSecond.getID
Print uThird.getID

Differences from QB:
See also:
Back to Procedures
Back to Variable Declarations
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki phatcode