bootybay

Understanding Static Variables

13 posts in this topic

#1 ·  Posted

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.

Share this post


Link to post
Share on other sites



#2 ·  Posted

So what is the problem you are experiencing?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

24 minutes ago, bootybay said:

I want to read the INI only once and from then on keep using the $Region values.

Your implementation of Static looks okay to me. Whatever the problem is, it's not due to a misunderstanding of Static.

Edited by czardas

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#5 ·  Posted

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

And yeah @Melba23 is right about adding Local, even though it should already work.

Edited by czardas

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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
1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#8 ·  Posted

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

@Jos Indeed, I see where you are coming from with that snippet. :) My wording is off. :whistle: On redeclaration the variable is initialized with the previous value which is held somewhere in memory. Is that any better?

Edited by czardas

Share this post


Link to post
Share on other sites

#10 ·  Posted

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

 

 


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#12 ·  Posted

Thank you all for all the super fast responses.

Your example clarified this for me @Melba23.

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