4NQ4D‖

iHDtz‖Static Statement

eod6b‖Declares a variable or an array at the procedure level within a subroutine or a function, so that the values of the variable or the array are retained after exiting the subroutine or function. Dim statement conventions are also valid.

Warning Icon

HD5Sa‖The Static statement cannot be used to define variable arrays. Arrays must be specified according to a fixed size.


SXakp‖Syntax:


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

xQqMt‖Example:


Sub ExampleStatic
Dim iCount As Integer, iResult As Integer
    For iCount = 0 To 2
        iResult = InitVar()
    Next iCount
QMQFs‖    MsgBox iResult,0,"The answer is"
End Sub
 
HEZbp‖' Function for initialization of the static variable
Function InitVar() As Integer
    Static iInit As Integer
8DkWE‖    Const iMinimum As Integer = 40 ' minimum return value of this function
q4DSE‖    If iInit = 0 Then ' check if initialized
        iInit = iMinimum
    Else
        iInit = iInit + 1
    End If
    InitVar = iInit
End Function