Static Statement

Declara unha variábel ou matriz a nivel de procedemendo dentro dun método ou función, de forma que os seus valores se manteñan despois da súa saída. As convencións da instrución Dim tamén son válidas.

Icona Aviso

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


Sintaxe:

Static NomeVar[(inicio To fin)] [As TipoVar], NomeVar2[(inicio To fin)] [As TipoVar], ...

Exemplo:

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 Función para o inicio da variábel estática

Function InitVar() As Integer

    Static iInit As Integer

    Const iMinimum as Integer = 40 REM valor de retorno mínimo desta función

    if iInit = 0 then REM comprobar se está inicializado

        iInit = iMinimum

    Else

        iInit = iInit + 1

    End If

    InitVar = iInit

End Function