I use ByRef a lot, but mostly to indicate that an array or object will be changed in the method.
In fact, until now, I thought it was necessary to use ByRef in order to "continue the changes to the object back to the calling code". However, after playing around with some code, including your example, I see that it's not necessary at all, which makes me think that it's useful as an "annotation", so that the programmer knows that the object WILL BE changed.
Now, I feel that the real "pass by reference" is the dot in front of the variable, and the ByRef is merely an indicator that this variable could be changed when returned.
Is this true? I'm not really sure what "passing a reference by reference" would produce. I came upon this article because I want several methods in a row to update a log file, which I sometimes add to the method signature as ByRef, and sometimes I don't, and I wanted to know why it still worked (i.e. my log file is created, and is updated in the calling method).
Here is your original example, but without the ByRef:
ClassMethod Test(UseByRef As %Boolean = {$$$YES})
{
Set Obj = {}
Set Obj.Property = 1
Write "At the begining ",Obj.Property,!
Do ..RefMethod(Obj)
Write "After Value pass ",Obj.Property,!
Do ..RefMethod(.Obj) // pass by reference because I want the object to change
Write "After ByRef pass ",Obj.Property,!
}
/// notice there's no ByRef in the method signature
ClassMethod RefMethod(Obj)
{
Set Obj = {}
Set Obj.Property = 500
}
USER>Do ##class(Utils.ObjByRef).Test()
At the begining 1
After Value pass 1
After ByRef pass 500
- Log in to post comments
