Static Statement

在子常式或函式中宣告程序級的變數或陣列,使得在結束子常式或函式後,這些變數或陣列的值仍能保留。Dim 陳述式慣例仍然有效。

警告圖示

Static 陳述式不能用於定義變數陣列,而只能用於定義固定大小的陣列。


語法


Static VarName[(start To end)] [As VarType], VarName2[(start To end)] [As VarType], ...

示例:


Sub ExampleStatic
Dim iCount As Integer, iResult As Integer
    For iCount = 0 To 2
        iResult = InitVar()
    Next iCount
    MsgBox iResult,0,"The answer is"
End Sub
 
REM 用於初始化靜態變數的函式
Function InitVar() As Integer
    Static iInit As Integer
    Const iMinimum as Integer = 40 REM minimum return value of this function
    if iInit = 0 then REM check if initialized
        iInit = iMinimum
    Else
        iInit = iInit + 1
    End If
    InitVar = iInit
End Function

Please support us!