Jump to content
Sign in to follow this  

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)

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

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

    $iState += 1 ;increment the state

; 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
Edited by RockemSockem

Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()

Share this post

Link to post
Share on other sites

Nice example. Covers all the basics. I didn't understand the difference between a static and a constant before, but now I do.

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post

Link to post
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
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.