byoda Posted July 6, 2010 Share Posted July 6, 2010 All, I was wondering if I could have a UDF as a variable in a function. Essentially, when the GO button is pushed, Func 3 is called, which in turn calls Func 2. I get an error message saying "Variable used without being declared on the "$test=GUICreate" line. When I take out the $test, it works, but I need to use the $test variable. This is why I am thinking I am not able to have a UDF as a variable, within a function. Thanks. Func 1() Case statement Case $msg=$Go Func 3() EndFunc Func 2() $test=GUICreate("Test", 300, 75) EndFunc Func 3() Func 2() EndFunc Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 6, 2010 Moderators Share Posted July 6, 2010 byoda, If you tidy up your code so it actually runs, you get no errors at all. Global $msg = 1, $go = 1 _1() Func _1() Switch $msg Case $msg = $go _3() EndSwitch EndFunc ;==>_1 Func _2() $test = GUICreate("Test", 300, 75) EndFunc ;==>_2 Func _3() _2() EndFunc ;==>_3 Now what was the question? 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...
byoda Posted July 6, 2010 Author Share Posted July 6, 2010 No, it doesnt. The $test = GUICreate("Test", 300, 75) runs great if: a. It is outside of a function. b. It is inside of the function, BUT the $test is removed, leaving GUICreate("Test", 300, 75) So what I am asking is, can you have a UDF as a variable, within a function? Is this legal? Thanks. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 6, 2010 Moderators Share Posted July 6, 2010 byoda,A light dawns perhaps! Do you have Opt("MustDeclareVars", 1) somewhere in your script? 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...
DicatoroftheUSA Posted July 6, 2010 Share Posted July 6, 2010 (edited) No, it doesnt. The $test = GUICreate("Test", 300, 75) runs great if:a. It is outside of a function.b. It is inside of the function, BUT the $test is removed, leaving GUICreate("Test", 300, 75)So what I am asking is, can you have a UDF as a variable, within a function? Is this legal? Thanks.inside the func, before guicreate.local $test Edited July 6, 2010 by DicatoroftheUSA Statism is violence, Taxation is theft. Autoit Wiki Link to comment Share on other sites More sharing options...
byoda Posted July 6, 2010 Author Share Posted July 6, 2010 Melba, Yes it was set. DIC, lol.... You got it good sir.... That worked. Could you elaborate why? Thanks. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 6, 2010 Moderators Share Posted July 6, 2010 byoda,Variables in AutoIt are of (essentially) 2 kinds - Global and Local (we will forget Static because it is still not fully resolved).Global variables are visible to the main section and any function in the code - Local variables are only visible within the function in which they are defined. AutoIt will automatically scope variables in the main script as Global and in functions as Local unless you tell it otherwise. Using Opt("MustDeclareVariables", 1) means that you MUST explicitly declare your variables as either Global or Local or you get an error. Look at this script:; This is the main script $vVar1 = 10 ; This is Global by default ; See what is visible in the function Test() ; See what is visible in the main script ConsoleWrite("In Main Script " & $vVar1 & @CRLF) ; This will work ConsoleWrite("In Main Script " & $vVar2 & @CRLF) ; This will give you an error Func Test() $vVar2 = 20 ; This is Local by default ; Both of these will work ConsoleWrite("In Function " & $vVar1 & @CRLF) ConsoleWrite("In Function " & $vVar2 & @CRLF) EndFuncNow let us declare $vVar2 as Global and see what happens:; This is the main script $vVar1 = 10 ; This is Global by default ; See what is visible in the function Test() ; See what is visible in the main script ConsoleWrite("In Main Script " & $vVar1 & @CRLF) ; This will work ConsoleWrite("In Main Script " & $vVar2 & @CRLF) ; This will give you an error Func Test() Global $vVar2 = 20 ; This is Local by default, but declared Global <<<<<<<<<<<<<<<<<<<<<<<<<<<< ; Both of these will work ConsoleWrite("In Function " & $vVar1 & @CRLF) ConsoleWrite("In Function " & $vVar2 & @CRLF) EndFuncAnd we can now see the variable outside the function. All clear?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...
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