Jump to content

Understanding Static Variables


Recommended Posts

I'm having problems grasping some concepts. I'll show you what I want to try and if it makes sense to you.

Func Stat()
    Static $Region = StringSplit(StringStripWS (IniRead("win7.ini", "Update", "Complete", "600, 500, 620, 520, 0x00FF00"), 8), ",", 2)
    PixelSearch($Region[0], $Region[1], $Region[2], $Region[3], $Region[4])
    If Not @error Then Return True
EndFunc

Func Test()
    While True
        If Stat() = True Then ExitLoop
        Sleep(5000)
    WEnd
    MsgBox(0, "Update", "complete!")
EndFunc

 

So Stat() gets called over and over again. I want to read the INI only once and from then on keep using the $Region values.

I don't want to declare a global or pass the array as ByRef into stat. 

Basically I want to IniRead the values while $Region is empty and then once it got its values not do it anymore. I want to keep it contained in Stat.

But I don't know how to do this.

Please let me know if I need to clarify this a little more.

Link to comment
Share on other sites

  • Developers
32 minutes ago, bootybay said:

So Stat() gets called over and over again. I want to read the INI only once and from then on keep using the $Region values.

I don't want to declare a global or pass the array as ByRef into stat. 

To me these 2 lines contradict each other: When you want a variable to be set ones, it will have to be global as any local variable only exists till the FUNC ends.

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Moderators

bootybay,

Your code looks fine to me - but I would suggest using "Local Static" to declare the array just to remind yourself that it is not available outside the function.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Developers
6 minutes ago, czardas said:

The static variable here ought to be kept in memory but not available to other functions. That's how it works for me anyway.

Are you sure? Try this little snippet which shows it has to be defined each time or am I missing something?:

For $x = 1 To 5
    Test()
Next

Func Test()
    If IsDeclared("testvar") Then
        ConsoleWrite("Already defined" & @CRLF)
    Else
        ConsoleWrite("Initialise $Testvar" & @CRLF)
        Static $testvar = "123"
    EndIf
    If IsDeclared("testvar") Then
        ConsoleWrite("testvar defined" & @CRLF & @CRLF)
    EndIf
EndFunc   ;==>Test

 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Moderators

Jos,

To show it works:

Global $iGlobal_Count = 0

For $i = 1 To 5

    $iGlobal_Count += 1

    _Test()

Next

Func _Test()

    Local Static $iStatic_Count = $iGlobal_Count ; Should only be executed once and retain the value 1 on subsequent calls

    ConsoleWrite($iGlobal_Count & " - " & $iStatic_Count & @CRLF)

EndFunc

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Developers

Ah.. that is how it works. That is confusion for my brain.
It doesn't actually exists but when you re-declare it, it will take the value of the initial declaration. 
Guess it is clear I have never used it as yet and probably won't in the future. :)

Jos

 

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I've only used it a couple of times myself. It's definately worth having, even if it's not entirely necessary. :)

Actually, it takes the last value prior to exiting the function (on the previous call); not the initial value (which may have been modified).

Edited by czardas
Link to comment
Share on other sites

One more example: demonstrating further application (the result of modifying a static variable).

Global Const $iInit = 11

For $x = 11 To 15
    Test()
Next

Func Test()
    Local Static $iCount = $iInit
    ConsoleWrite($iCount & @LF)
    $iCount += 1
EndFunc   ;==>Test

 

Edited by czardas
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...