STATIC statement

Purpose | Declare static variables inside a Sub, Function, Method, or Property. Static variables retain their values as long as the program is running.
Syntax | STATIC variable[()] [AS type] [, variable[()]] STATIC variable[()] [, variable[()]] [, ...] AS type

Remarks

The STATIC statement is valid only inside a procedure. Static variables retain their values even after the procedure ends. A static variable is local to its procedure, and can have the same name as other variables in other parts of the program without conflict.

To declare an array as a static variable, use an empty set of parentheses in the variable list: You can then use the DIM statement to dimension the array.

STATIC MyArray%()

STATIC StringArray() AS STRING

The STATIC statement may, optionally, accept a list of variables, all of which are defined by the type descriptor keyword that follows them. For example:

STATIC aaa, bbb, ccc AS INTEGER

STATIC vptr, aptr() AS LONG PTR

Restrictions | DEFtype has no effect on variables defined by a STATIC statement.
See also | DIM, GLOBAL, LOCAL, THREADED

Example

#COMPILE EXE

#DIM ALL

#INCLUDE "WIN32API.INC"

DECLARE SUB DoMessage ()

FUNCTION PBMAIN

DIM z%

FOR z% = 1 TO 5

DoMessage

NEXT z%

END FUNCTION

SUB DoMessage ()

STATIC x AS INTEGER

STATIC Message() AS ASCIIZ * 256

DIM Message(1 TO 5) AS STATIC ASCIIZ * 256

INCR x 'add one to x

Message(x) = "X =" + STR$(x)

#IF %DEF(%PB_CC32)

PRINT Message(x)

#ELSE

MSGBOX Message(x)

#ENDIF

END SUB