Unsigned Posted September 26, 2011 Share Posted September 26, 2011 I wrote this up real quick to sort out some questions I had about AutoIt's variable scope. Did you know For loops within functions cannot use a Global as the iterator variable? I didn't. Posting it here in case it can enlighten anybody else. It runs several tests, displaying the results of each test individually. expandcollapse popupMsgBox(0, "Info", "global scope is when execution is NOT inside a function." & @CR & "local scope is any time execution IS inside a function.") Global $globalglobal = 2 Func GlobalFunc1($value) Local $globalglobal = $value EndFunc GlobalFunc1(10) AssertError("'Local' in local scope will create a 'Local'", $globalglobal <> 2) Global $globalset = 2 Func GlobalFunc2($value) Global $globalset = $value EndFunc GlobalFunc2(10) AssertError("'Global' in local scope creates/accesses a 'Global'", $globalset <> 10) Dim $dimglobal = 2 Local $globallocal = 2 Func LocalGlobalFunc($value) $dimglobal = $value $globallocal = $value EndFunc LocalGlobalFunc(10) AssertError("'Dim' or 'Local' in global scope will always create 'Global' anyway", ($dimglobal <> 10) Or ($globallocal <> 10)) For $globalindex = 1 to 4 ; $globalindex will be created as a Global Local $junk = "something" Next Global $lastglobalindex = $globalindex Func GlobalLoop1($value) Global $globalindex $globalindex += $value EndFunc GlobalLoop1(99) AssertError("loop indexes in global scope are always 'Global'", $globalindex <> ($lastglobalindex + 99)) Global $globalloop = 99 Func LoopFunc($last) Global $globalloop ; This declaration should have no effect on the loop variable below Local $firstgloballoop = $globalloop For $globalloop = 1 to $last ; The index variable here will be Local notwithstanding our previous attempt to make it reference the existing Global Local $blah = "something" Next Local $retarray[2] $retarray[0] = $firstgloballoop $retarray[1] = $globalloop Return $retarray EndFunc Global $theretarray = LoopFunc(3) AssertError("loop indexes in local scope will always be 'Local', even to the point of ignoring/overriding any prior 'Global' declarations", ($theretarray[0] <> 99) Or ($theretarray[1] <> 4)) Global $array[10] $array[0] = "hi" Func ArrayFunc($value) Dim $array[5] $array[0] = $value EndFunc ArrayFunc("lol") AssertError("'Dim' in local scope will use a pre-existing 'Global' if it exists, otherwise create a 'Local'", $array[0] <> "lol") MsgBox(0, "Done", "Thats the end of the tests!") Exit ; Used to display test results only Func AssertError($name, $bool) If $bool Then Return MsgBox(16, "Test Results", '' & $name & ' = FAIL') Return MsgBox(64, "Test Results", '' & $name & ' = SUCCESS') EndFunc . Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 27, 2011 Moderators Share Posted September 27, 2011 UCL, Did you know For loops within functions cannot use a Global as the iterator variable? I didn'tThen you should have read the Help file more carefully: For...To...Step...Next variable - The variable used for the count. Remarks The Variable will be created automatically with a LOCAL scope, even when MustDeclareVars is on. And why would you want the actual count variable to be Global anyway - you reset it each time you start the loop! You can of course use a Global variable as the Start or Stop value - which actually serves a useful purpose. Have you read the Variables - using Global, Local and ByRef tutorial in the Wiki - it explains scope in some detail. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted September 27, 2011 Share Posted September 27, 2011 Also don't use Dim. Use Local or Global so you know where things are. Unsigned 1 .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
Unsigned Posted September 29, 2011 Author Share Posted September 29, 2011 (edited) Then you should have read the Help file more carefully: Haha Barring some dramatically new syntax, I don't usually read exhaustively on every basic construct for a new language. (After all, most languages out there (that I use regularly) fall into two categories, C-like and BASIC-like.) Good to know that it's in the manual though! I just found the script useful to illustrate the behavior (a little code can be faster than searching the manual sometimes ) Edited September 29, 2011 by Unsigned . Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now