Static-instructie

Declareert een variabele of een array op het procedureniveau binnen een Sub of Function zodat de waarden van de variabele of de array worden behouden na het beëindigen van de Sub of Function. Dim-instructieregels zijn ook geldig.

Waarschuwingspictogram

De Static-instructie kan niet worden gebruikt om variabele arrays te definiëren. Arrays moeten worden gespecificeerd overeenkomstig een vaste grootte.


Syntaxis:

Static VarNaam[(Start to Einde)] [As VarType], VarNaam2[(Start to Einde)] [As VarType], ...

Voorbeeld:

Sub ExampleStatic

Dim iCount As Integer, iResult As Integer

    For iCount = 0 To 2

        iResult = InitVar()

    Next iCount

    MsgBox iResult,0,"Het antwoord is"

End Sub

 

' Functie voor initialiseren van de Static-variabele

Function InitVar() As Integer

    Static iInit As Integer

    Const iMinimum as Integer = 40 ' minimale retourwaarde van deze functie

    if iInit = 0 then ' controle indien geïnitialiseerd

        iInit = iMinimum

    Else

        iInit = iInit + 1

    End If

    InitVar = iInit

End Function