警告: 这个特性目前只是实验性的.它可能不能正常工作,可能含有Bug或者随时被修改或者移除并没有任何通知.

请不要报告关于此特性的BUG或者特性请求.

使用此特性后果自负(USE AT YOUR OWN RISK).

关键字参考


Static

定义一个静态变量或者静态数组.

Static [范围] $variable [ = 初始化 ]
Static [范围] $array[子脚本 1]...[子脚本 n] [ = 初始化 ]

参数

范围 一个可选修饰, Local 或者 Global that indicates where the variable is visible.
$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.
子脚本 要创建数组定义的元素数量, 索引为 0 到 n-1.

注意/说明

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 specifier Local is used, then the static variable is visible and usable only in the function in which it is declared and it's resolved in the environment of execution (logical scope). This means that conditionally declared variable is visible only when declaraion condition is met. If the scope specifier 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 specifier 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. You can not change a static variable to a local or global variable nor vice-versa.


If you want to resize an array use ReDim.

相关

Local, UBound, ReDim, AutoItSetOption

示例/演示


; Call the Example function to initialize the Static variable in Local scope.
Example()

; Call the Example function a second time to show that the variable has retained the data we last assigned to it.
Example()

Func Example()
    Local Static $sString = "This is a line of text which is declared using a Static variable in Local scope." & @CRLF & @CRLF & _
            "The variable $sString will now be visible to this function only and until the script closes."
    MsgBox(4096, "", $sString)
    $sString = "If using just Local scope this string wouldn't be visible if this function was called multiple times, but since we're using the Static keyword" & @CRLF & _
            "the variable $sString will retain the data last assigned to it."
EndFunc   ;==>Example