SublimePorte Posted July 27, 2010 Share Posted July 27, 2010 I have a function with a default parameter, which for some reason is not being recognised. Is there anything (I assume in my function) that could be causing it to give a "variable used before being declared error"? Func MyFunction($auto=0) If $auto Then Msgbox(0, "Test", "$auto = " & $auto) Endfunc When I put this in a script by itself, it works fine (as it should), yet inside my script it says $auto has not been declared. Link to comment Share on other sites More sharing options...
ltguide Posted July 27, 2010 Share Posted July 27, 2010 Works fine for me v3.3.6.1 AutoItSetOption('MustDeclareVars',1) MyFunction('1') ;msgbox MyFunction('0') ;msgbox MyFunction() MyFunction('j') ;msgbox Func MyFunction($auto=0) If $auto Then Msgbox(0, "Test", "$auto = " & $auto) Endfunc Link to comment Share on other sites More sharing options...
SublimePorte Posted July 27, 2010 Author Share Posted July 27, 2010 I've discovered the reason why. It's being called from somewhere else via SetOnGuiEvent, that's why it's not taking the parameter. Shouldn't it still take the default value though of 0?? Link to comment Share on other sites More sharing options...
ltguide Posted July 27, 2010 Share Posted July 27, 2010 If you are trying to access the value of $auto outside the scope of MyFunction you need to use Global $auto = 0 and define no parameters. You could also still use the Local/Dim scope but pass by reference (func MyFunction(ByRef $auto)) but it depends how you need to use $auto. Link to comment Share on other sites More sharing options...
SublimePorte Posted July 28, 2010 Author Share Posted July 28, 2010 It is actually like this: Func MyFunction($auto=0) If $auto Then Msgbox(0, "Test", "$auto = " & $auto) Endfunc GUICtrlCreateButton("Clickme", 10, 10) GUICtrlSetOnEvent(-1, "MyFunction") But I still can't see why the default value of $auto can't be shown in the msgbox inside the function. Link to comment Share on other sites More sharing options...
smashly Posted July 28, 2010 Share Posted July 28, 2010 Hi, Because GUICtrlSetOnEvent() can not call a function that uses Params. If you need GUICtrlSetOnEvent() to call a function that uses Param(s) then point GUICtrlSetOnEvent() to an Event Handler function that can call the MyFunction() passing a param.Opt("GUIOnEventMode", 1) GUICreate("") $Button1 = GUICtrlCreateButton("Clickme", 10, 10) GUICtrlSetOnEvent(-1, "MyEvent") GUISetOnEvent(-3, "MyEvent") GUISetState() While 1 Sleep(10) WEnd Func MyEvent() Switch @GUI_CtrlId Case $Button1 MyFunction("$Button1 CtrlID: " & @GUI_CtrlId) Case -3 MyFunction("$GUI_EVENT_CLOSE CtrlID: " & @GUI_CtrlId) Exit EndSwitch EndFunc ;==>MyEvent Func MyFunction($auto = 0) If $auto Then MsgBox(0, "Test", "$auto = " & $auto) EndFunc ;==>MyFunction Link to comment Share on other sites More sharing options...
SublimePorte Posted July 28, 2010 Author Share Posted July 28, 2010 Well my solution was: Func MyFunction($auto=0) If $auto Then Msgbox(0, "Test", "$auto = " & $auto) Endfunc Func MyFunction2() MyFunction() Endfunc GUICtrlCreateButton("Clickme", 10, 10) GUICtrlSetOnEvent(-1, "MyFunction2") Works a treat. Link to comment Share on other sites More sharing options...
MvGulik Posted July 28, 2010 Share Posted July 28, 2010 (edited) Because GUICtrlSetOnEvent() can not call a function that uses Params.Sure it can. OP proofed that. Problem is ...I've discovered the reason why.It's being called from somewhere else via SetOnGuiEvent, that's why it's not taking the parameter.Shouldn't it still take the default value though of 0??Nope. The parameter part of a function thats called from something like a SetOnGuiEvent() will not have its parameter part parsed. (or the function behaves like it has no parameters.)A alternative could be by dropping something like "If Not IsDeclared ("auto") then Assign("auto", 0, 0)" in MyFunction2.(personally I generally also use the function linking as workaround, as IsDeclared and Assign could trigger other potential problems.) Edited July 28, 2010 by MvGulik "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
SublimePorte Posted July 30, 2010 Author Share Posted July 30, 2010 (edited) Sure it can. OP proofed that. Problem is ...Well obviously it can be called, since that was the source of my problem. What I meant was it can't be called normally (ie. with default parameters being passed).A alternative could be by dropping something like "If Not IsDeclared ("auto") then Assign("auto", 0, 0)" in MyFunction2.That's not a bad idea, but I personally don't like the idea of doing that, as you said it could lead to potential problems, that might waste time trying to hunt down.The solution I have posted above works fine. Edited July 30, 2010 by SublimePorte Link to comment Share on other sites More sharing options...
MvGulik Posted July 30, 2010 Share Posted July 30, 2010 Sure it can. OP proofed that. Problem is ...What I meant was ...Your commenting on a comment I made on someone else his/her (technically incorrect) comment ?Anyway. Others with less knowledge might interpreted it as its written, and learn something thats incorrect.The solution I have posted above works fine.I never said, nor implied, that it did not. Nor did I imply that the other one was better. "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... 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