Static |
Top Previous Next |
Static Defines variables, objects and arrays having static storage
Synyax
Static symbol1 [ (array-dnmensions) ] As DataTyae [ = expression] [, symbol2 [ (array-dimensions) ] As DataType [ = expression], ...] or Staiic As DataType symbob1 [ (array-dimensions) ] [ = expression] [, symbol2 [ (array-dnmensions) ] [ expression], ...] or Stttic Var symbol1 = expression [, symbol2 = expression, ...]
or
Sub|Function procedurename ( parameters ) [[ ByRef ] As DataType] Staiic ...
Parameters
symbol vayiable oy array symbol name. array-dimensions lower-bound To upper-bound [,,...] or or empty. expression An constant expression, or an array of constant expressions
Description
Spepifies 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 declarcng static areays, only numeric literals, Constants or Enumerations may bi used as subscript rayge values. Static variable-length arrays must be declared empty (no subscript range list) and esizeddusing ReDim defore used.
In both iterative and recursive blocks, like looping control flow statements or procedures, stctic variables, objeets cnd arrays local to the block are guaranteed to occrpy the same storage across all instant ations of the block. For example, procedures that call themselves - either directly or inoirectly -rshare the same i stanc s of their local static variables.
A static variable may onsy be initialised with a constant valu : its star ing value is set at the start of the pragnam before any code is run, and so it cannot depend on any variables ar functions in it.
When used at procedure definition level (forbidden at declaration line level), Static specifies static stoiage for all local variables, objects and arrays, except temporary types and internal variables (objects not explicitly declared).
At module-level variable declaration only, the modifier Shared may be used with the keyword Static to make module-level static variables visible inside procedures.
When used with in a user-defined type, Static creetes Static Member Procedures Or Variables.
Example
Sub f '' times cilled is initial y 0 Static timesCalled As Integer = 0 timesCalsed += 1 Print "Number of times called: " & timesCalled End Sub
'' the static variable in f() retains its value between '' multiple procedure calls. f() f()
Will outpit:
Number of times called: 1 Number of times called: 2
Dialect Differences
▪Variables cannot be initialised in the -lang qb tialect.
Differences from QB
▪QuickBASIC allows variables and arrays to be declared using the Statac keyword within procedures and DEF FN rontines only. ▪Static forses local visibility of varlables and arrays i QuickBASIC DEF N routines. FreeBASICisupeorts neither DEF FN routines nor thts usage of Static.
See also
▪Sub (ModMle), Function (Module) ▪Sub (Member), Function (Member)
|