Jump to content

Static example


Recommended Posts

Here's some examples of how a 'Static' variable will play out (from the new beta)

get the beta: http://www.autoitscript.com/autoit3/files/beta/autoit

I couldn't wrap my head around how static variables worked until I read this (the example in the help file was confusing) discussion

;------------------------------------
; Example 1
;------------------------------------
Example1("First value") ;persistent values
Example1("A different value")

Func Example1($sSet)
    Static $sValue = $sSet
    MsgBox(0, 'The value is', $sValue)
EndFunc

;------------------------------------
; Example 2
;------------------------------------
Global $bFinished = False
While $bFinished = False
    Example2() ;Recursive
WEnd

Func Example2()
    Static $iState = 1

    Switch $iState
        Case 1
            ConsoleWrite("First call" &@LF)
        Case 2
            ConsoleWrite("Second call" &@LF)
        Case 3
            ConsoleWrite("Third call" &@LF)
        Case 4
            ConsoleWrite("Done" &@LF)
            $bFinished = True
    EndSwitch

    $iState += 1 ;increment the state
EndFunc


;------------------------------------
; Example 3
;------------------------------------
#include <array.au3>

;----- before
$aArray1 = Example3()
_ArrayDisplay($aArray1, "Array1: Default")


Example3(1, "One")
Example3(3, "Three")
Example3(5, "Five")
Example3(7, "Seven")
$aArray2 = Example3(9, "Nine")
_ArrayDisplay($aArray2, "Array2: Changed")

;------ after
$aArray1 = Example3()
_ArrayDisplay($aArray1, "Array1: Changed")


Func Example3($iIndex=-1, $vValue="")
    Static $aArray[10] = [0,1,2,3,4,5,6,7,8,9]

    If $iIndex > -1 And $iIndex < 10 Then $aArray[$iIndex] = $vValue

    Return $aArray
EndFunc
Edited by RockemSockem
Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...