Declare a static variable or create a static array.
Static [Scope] $variable [ = initializer ]
Static [Scope] $array[subscript 1]...[subscript n] [ = initializer ]
| 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. |
Static $a, $b, $c
Static $a = 2, $b = 10, $c = 20
; 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()