Skip to content

STATIC

Source: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgStatic Last revised: 2021-08-31

Defines variables, objects and arrays having static storage.

Syntax

Static symbol1 [(array-dimensions)] As DataType [= expression] [, symbol2 ...]

or

Static As DataType symbol1 [(array-dimensions)] [= expression] [, symbol2 ...]

or

Static Var symbol1 = expression [, symbol2 = expression, ...]

or (at procedure level):

Sub|Function procedurename ( parameters ) [As DataType] Static
    ...
End Sub|Function

Parameters

  • symbol — Variable or array symbol name.
  • array-dimensionslower-bound To upper-bound [, ...] or Any [, Any...] or empty.
  • expression — A constant expression, or an array of constant expressions.

Description

Specifies 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 numeric literals, constants or enumerations may be used as subscript range values. Static variable-length arrays must be declared empty (no subscript range list) and resized using Redim before used.

In both iterative and recursive blocks, like looping 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.

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.

When used at procedure definition level, Static specifies static storage for all local variables, objects and arrays.

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.

Examples

vb
Sub f
    '' times called is initially 0
    Static timesCalled As Integer = 0
    timesCalled += 1
    Print "Number of times called: " & timesCalled
End Sub

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

Output:

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

Dialect Differences

  • Variables cannot be initialised in the -lang qb dialect.

Differences from QB

  • QuickBASIC allows variables and arrays to be declared using the Static keyword within procedures and DEF FN routines only.
  • Static forces local visibility of variables and arrays in QuickBASIC DEF FN routines. FreeBASIC supports neither DEF FN routines nor this usage of Static.

See Also

Translated from FreeBASIC official docs. Contact us for removal if infringed.
FreeBASIC is an open-source project, not affiliated with Microsoft