Static


Syntax
Static[.<type>] <variable[.<type>]> [= <constant expression>] [, ...]
Description
Static allows to create a local persistent variable in a Procedure even if the same variable has been declared as Global in the main program. If a type is specified after Static, the default type is changed for this declaration. Static can also be used with arrays, lists and maps. When declaring a static array, the dimension parameter has to be a constant value.

The value of the variable isn't reinitialized at each procedure call, means you can use local variables parallel to global variables (with the same name), and both will keep their values. Each variable can have a default value directly assigned to it, but it has to be a constant value.

Beside Static you can use the keyword Protected, to separate global from local variables, but with Protected the local variables will not keep their values.

Example: With variable

  Global a
  a = 10
  
  Procedure Change()
    Static a
    a+1
    Debug "In Procedure: "+Str(a) ; Will print 1, 2, 3 as the variable increments at each procedure call.
  EndProcedure 
  
  Change()
  Change()
  Change()
  Debug a ; Will print 10, as the static variable doesn't affect global one.

Example: With array

  Global Dim Array(2)
  Array(0) = 10
  
  Procedure Change()
    Static Dim Array(2)
    Array(0)+1
    Debug "In Procedure: "+Str(Array(0)) ; Will print 1, 2, 3 as the value of the array field increments at each procedure call.
  EndProcedure 
  
  Change()
  Change()
  Change()
  Debug Array(0) ; Will print 10, as the static array doesn't affect global one.

Example: With multiple procedure

  Procedure Foo()
    Static x = 100  ; the declaration and the assignment is done only once at program start.
  
    Debug x
    x + 1
  EndProcedure

  Foo() ; Display 100
  Foo() ; Display 101
  Foo() ; Display 102

  Debug "---"

  Procedure Bar()
    Static x  ; the declaration is done only once at program start.  
    x = 100   ; the assignment is done on every Procedure call.
  
    Debug x
    x + 1
  EndProcedure

  Bar() ; Display 100
  Bar() ; Display 100
  Bar() ; Display 100