Optional ByRef

In my method I have a variable that I want to have as a ByRef. But I do not always need it to be set how can I make ByRef optional. Thanks so much:)

AFAIK, you can’t make it optional, but just return the same value that is passed when the var. doesn’t need to be changed.

I think the only way is to overload the method.

Or you could do what Kem said. Thanks

Thankyou so much, this is a great technique which I should have thought of. :slight_smile:

Do you even need to do overloading?

All objects are passed by reference, there is no “by value” version.

Value type however can be passed by value or by reference - reference here just means the original variable or property is sent and by value means a copy is made, and that copy is sent instead.

So, if you have a method that has to do an action on a value type but you do not want the particular variable or property to be altered every time, simply pass it byval instead of byref as part of the calling parameters?.

Declaring byval /byref in the method declaration should just be there to tell xojo what the default sending mechanism is when you omit “byval /byref” when making the call (so you dont have to write byref /byval for every parameter when you call a method)

@Tony Stark: there are times when it does make sense to pass an object byref. It allows you to replace the original reference, not just modify the object itself.

I don’t think overloading will work in this situation. If the only difference is byref vs. byval, I’m pretty sure you’ll get a compile error.

[quote=26377:@Tim Hare]@Tony Stark: there are times when it does make sense to pass an object byref. It allows you to replace the original reference, not just modify the object itself.[/quote]I thought Xojo ignored byval for objects and always sent it byref?

Nope. You can pass objects both ways. ByVal means “pass a reference to the object”. ByRef means “pass a pointer to the reference to the object.”

[quote=26379:@Tim Hare]Nope. You can pass objects both ways. ByVal means “pass a reference to the object”. ByRef means “pass a pointer to the reference to the object.”[/quote]Well the doc says:

[quote]
All arrays and object types (including arrays) are always passed by reference regardless of whether you have specified to use ByVal[/quote]
http://documentation.xojo.com/index.php/ByRef

The docs are definitely misleading on this. An object is just a normal value like Integer or Double, but behind the scenes it’s a pointer to the actual bytes that make up the object. ByRef acts the same for everything in that you pass it a variable and it can modify the value stored in the variable.

Put another way,

when you pass an object reference ByVal, you pass the object ByRef and the reference ByVal. Ie.,

sub somemethod(c as class1)
   c = new class1
end

leaves the original object reference unchanged.

when you pass a object reference ByRef, you pass the object ByRef and the reference ByRef. ie.,

sub somemethod(byref c as class1)
  c = new class1
end

replaces the original object reference with a reference to a brand new object.

Tim

1 Like