Warning: This feature is experimental. It may not work, may contain bugs or may be changed or removed without notice.

DO NOT REPORT BUGS OR REQUEST NEW FEATURES FOR THIS FEATURE.

USE AT YOUR OWN RISK.

Keyword Reference


Static

Declare a static variable or create a static array.

Static [Scope] $variable [ = initializer ]
Static [Scope] $array[subscript 1]...[subscript n] [ = initializer ]

Parameters

Scope An optional modifier, Local or Global that indicates where the variable is visible.
$variable The name of the static variable to declare.
initializer The value that will be initially assigned to the variable. The initializer can be a function call of involve mathematical or string operations. This initializer is only evaluated the first time this variable declaration is encountered.
subscript The number of elements to create for the array dimension, indexed 0 to n-1.

Remarks

The Static keyword can appear on the line before the optional scope specifier, or after. e.g. Local Static or Static Local are both acceptable.

If the scope modifier Local is used, then the static variable is visible and usable only in the function in which it is declared. If the scope modifier Global is used, then the static variable is visible and usable in all parts of the script; in this regard, a Global Static has very little difference from a Global variable. If the scope modifier is not used, then the static variable will be created in the local scope; in this way, Static is similar to Dim.

The difference between Local and Static is variable lifetime. Local variables are only stored while the function is called and are visible only within the function in which they are declared; when the function returns, all its local variables are released. Static variables are likewise visible only in the function in which they are declared, but they continue to exist, retaining their last value, after the function finishes execution. When looking for variables, the local scope is checked first and then the global scope second.

The Static keyword performs similar functions to the Global/Local/Dim keywords.
  1. They all declare a variable before you use it.

  2. They all can create an array.


Note: Static variables must be declared using the Static keyword prior to use, no matter how AutoItSetOption("MustDeclareVars") is set. Static variables can not be Const.

You can also declare multiple static variables on a single line:

Static $a, $b, $c


And initialize the variables:

Static $a = 2, $b = 10, $c = 20



When initializing a static variable, the initialization value is evaluated and assigned only the first time, when the variable is created. On all subsequent passes, the initializer is ignored.

See Local for more information about using arrays, which has all the same functionality as in Local, except for:
  1. Reinitalizing a Static variable has no effect.

  2. Changing the size of a Static array is treated like a ReDim.

  3. You can not change a static variable to a local or global variable nor vice-versa.


If you want to resize an array, always use Static to do so, not Redim.

Related

Local, UBound, ReDim, AutoItSetOption

Example


; Static variables examples.

Func Test1()
    Static $STbFirstPass = 1

    If $STbFirstPass Then
        $STbFirstPass = 0
        ; Perform tasks for the first time through
    EndIf
    ; Other things the function should do
EndFunc   ;==>Test1

Func Accumulate($State)
    Static $Values[9]
    Local $I

    If IsNumber($State) Then
        Switch $State
            Case -1
                ; Reset
                For $I = 0 To 8
                    $Values[$I] = 0
                Next
                Return True
            Case -2
                Return $Values
            Case 0 To UBound($Values) - 1
                $Values[$State] += 1
                Return $Values[$State]
            Case Else
                If $State < 0 Then
                    SetError(1, 0)
                    Return False
                Else
                    Static $Values[$State + 1] ; Resize the array to accomodate the new value
                    $Values[$State] = 1
                    Return 1
                EndIf
        EndSwitch
    Else
        SetError(2, 0)
    EndIf
EndFunc   ;==>Accumulate

Global $I

Test1()

For $I = 1 To 99
    Accumulate(Random(0, 20, 1))
Next
For $I In Accumulate(-2)
    ConsoleWrite($I & ", ")
Next
ConsoleWrite("\n");

Test1()