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

ご支援をお願いします!