Sign in to follow this  
Followers 0
JohnBailey

ByRef Optional Parameter

6 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 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.

 

Share this post


Link to post
Share on other sites

You can't have a optional parameter with byref

Gary, 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

Share this post


Link to post
Share on other sites

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)


NEWHeader1.png

Share this post


Link to post
Share on other sites

1 using ByRef

2 Uses Global

8)

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

Share this post


Link to post
Share on other sites

thanks again guys.


A decision is a powerful thing

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0