JohnBailey Posted September 24, 2007 Share Posted September 24, 2007 How do I make a ByRef optional parameter of a function For instance the following does not work, but how do I make the concept work. Local $a = "Hello" Local $b = "Hi back at ya" example($a) example($a,$b) Func example(byRef $var1, byRef $var2 = 0) MsgBox(0,'Var One Says',$var1) If $var2 <> 0 Then MsgBox(0,'Var Two Says Back',$var2) EndIf EndFunc IF you try that it gives you two errors ... hopefully it's obvious. A decision is a powerful thing Link to comment Share on other sites More sharing options...
GaryFrost Posted September 24, 2007 Share Posted September 24, 2007 (edited) How do I make a ByRef optional parameter of a function For instance the following does not work, but how do I make the concept work. Local $a = "Hello" Local $b = "Hi back at ya" example($a) example($a,$b) Func example(byRef $var1, byRef $var2 = 0) MsgBox(0,'Var One Says',$var1) If $var2 <> 0 Then MsgBox(0,'Var Two Says Back',$var2) EndIf EndFunc IF you try that it gives you two errors ... hopefully it's obvious. You can't have a optional parameter with byref The ByRef keyword is optional and means: (1) the parameter must a variable, and (2) the original variable is linked to the parameter, so that any changes to the parameter inside the function, would affect the original variable as well. By default, a parameter is passed by value which means that a copy of the parameter's value, is manipulated by the function. Edited September 24, 2007 by GaryFrost SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference. Link to comment Share on other sites More sharing options...
JohnBailey Posted September 24, 2007 Author Share Posted September 24, 2007 You can't have a optional parameter with byrefGary, first of all thank you for your patience and explanation. I actually have read that several times, but I was wondering if there is some way that this can be accomplished... not doing what I'm doing obviously, but if there was a better way to accomplish that. A decision is a powerful thing Link to comment Share on other sites More sharing options...
Valuater Posted September 24, 2007 Share Posted September 24, 2007 1 using ByRef Local $a = "Hello" Local $b = "Hi back at ya" ;example($a,) ;remove example($a, $B) Func example(ByRef $a, ByRef $B) ; by ref means $b, not $b = anything Local $var1 = $a, $var2 = $b MsgBox(0, 'Var One Says', $var1) If $var2 <> "" Then MsgBox(0, 'Var Two Says Back', $var2) EndIf EndFunc ;==>exampleoÝ÷ Ûe,zÁ¥¡¶¥jëh×6Global $a = "Hello" Global $b = "Hi back at ya" example() example(1) Func example($var = "") ; by ref means $b, not $b = anything MsgBox(0, 'Var One Says', $a) If $var <> "" Then MsgBox(0, 'Var Two Says Back', $B) EndIf EndFunc ;==>example 8) Link to comment Share on other sites More sharing options...
JohnBailey Posted September 24, 2007 Author Share Posted September 24, 2007 1 using ByRef2 Uses Global8)Valuater, thank you also! I had been doing those two methods and they work great. It's good to here from you and Gary saying basically that's the way to do it! I just didn't know if there was a better method. A decision is a powerful thing Link to comment Share on other sites More sharing options...
JohnBailey Posted September 24, 2007 Author Share Posted September 24, 2007 thanks again guys. A decision is a powerful thing 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