Nahuel Posted September 4, 2008 Share Posted September 4, 2008 I'm guessing that this behaviour is intended, but could someone explain why? If I create a function with just one optional parameter and this function is called from an event control, it works as long as I don't use the optional parameter inside the function. Here's an example: Opt("GUIOnEventMode",1) GUICreate("Test",200,100) GUICtrlCreateButton("no param",0,0,100,100) GUICtrlSetOnEvent(-1,"testfunc_1") GUICtrlCreateButton("with param",100,0,100,100) GUICtrlSetOnEvent(-1,"testfunc_2") GUISetState() While 1 Sleep(100) WEnd Func testfunc_1($optparam="") MsgBox(0,"","Works! didn't use the optional parameter inside function") EndFunc Func testfunc_2($optparam="") If $optparam="" Then Return MsgBox(0,"","Empty string!") MsgBox(0,"",$optparam) EndFunc I know that GUiCtrlSetOnEvent() can't call a function with parameters, but why are they totally ignored even if they are optional? It's like parameter variables are not declared when the udf is registered for an onevent call? I wanted to create a function that could be called without parameters from a control and with parameters from other parts of the script. Of course I came up with a workaround, so this isn't really a problem but a curiosity. Link to comment Share on other sites More sharing options...
enaiman Posted September 5, 2008 Share Posted September 5, 2008 Hello my friend I bet you didn't think of this way to script it: Opt("GUIOnEventMode",1) GUICreate("Test",200,100) GUICtrlCreateButton("Function Call",0,0,100,100) GUICtrlSetOnEvent(-1,"testfunc_1") GUICtrlCreateButton("Normal Call",100,0,100,100) GUICtrlSetOnEvent(-1,"testfunc_2") GUISetState() Global $optparam, $2ndparam While 1 Sleep(100) WEnd Func testfunc_1() testfunc_2("1st parameter", "2nd parameter") EndFunc Func testfunc_2($optparam = "", $2ndparam = "") If $optparam = "" And $2ndparam = "" Then MsgBox(0,"Button Click","Empty string!") Else MsgBox(0,"Function call",$optparam & " "&$2ndparam) EndIf EndFunc So - it can be done - you can use the function without parameters in the normal call (button press) and with parameters (function call). I had no idea if it were possible but you've made me think about it hehe - we both learned something out of this SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :) Link to comment Share on other sites More sharing options...
rasim Posted September 5, 2008 Share Posted September 5, 2008 enaimanI think will be better declare variableDim $optparam Opt("GUIOnEventMode",1) GUICreate("Test",200,100) GUICtrlCreateButton("no param",0,0,100,100) GUICtrlSetOnEvent(-1,"testfunc_1") GUICtrlCreateButton("with param",100,0,100,100) GUICtrlSetOnEvent(-1,"testfunc_2") GUISetState() While 1 Sleep(100) WEnd Func testfunc_1($optparam = "") MsgBox(0,"","Works! didn't use the optional parameter inside function") EndFunc Func testfunc_2($optparam = "") If $optparam = "" Then Return MsgBox(0,"","Empty string!") MsgBox(0,"",$optparam) EndFunc Link to comment Share on other sites More sharing options...
Nahuel Posted September 7, 2008 Author Share Posted September 7, 2008 Thank you guys! This wasn't really a problem, but a question. I thought it was weird, that's all. Martin's way is the best solution, IMO. I tried declaring the variable before but I got the "variable already declared as parameter" error. I wonder why this happens... Link to comment Share on other sites More sharing options...
enaiman Posted September 7, 2008 Share Posted September 7, 2008 It doesn't happen Nahuel - I guess you haven't tried my example or Rasim's; both of them work. You have to declare the variable first but after that everything is fine. Have a look at our examples. SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :) Link to comment Share on other sites More sharing options...
Nahuel Posted September 10, 2008 Author Share Posted September 10, 2008 It seems that I can't make myself clear I wasn't looking for a solution, I said I already have one. I just thought it was weird. Thank you guys for replying >_< 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