Search code examples
syntaxparameter-passingintersystems-cacheobjectscript

Intersystems cache Object Script pass property as parameter by reference


Given:

  • A class "A" with an %Integer property "intA"
  • A class "B" with a classMethod foo(ByRef num As %integer) that gets a parameter byRef and does some calculations.

And knowing that in caché Object Script:

  • If you want to pass a parameter by ref you need to put a dot '.' preceding the name of the variable passed by ref.
  • Inside a class, if you want to refer to your own properties you need to precede the name of the property with 2 dots '..'

How should I call classMethod foo if I want to pass the property "intA" byRef? Because preceding the property name with 3 dots seems to be not the correct way.

Code Snippet class B:

Class B Extends %RegisteredObject
{
///doubles num
ClassMethod foo(ByRef num As %Integer)
{
    set num = num*2
}
}

Code Snippet class A:

Class A Extends %RegisteredObject
{

Property intA As %Integer;

Method test()
{
    set ..intA= 5
    do ##class(B).foo(..intA)
    //If correctly passed by ref, ..intA should be 10, but it is still 5
}

}

Thanks in advance.


Solution

  • Only variables local or global, could be passed by reference. For properties it is not possible. You can pass property name as a string, and set value with $property method. And if you need to do it in class method or/and in other class, you should pass variable this as well. So your code could go a little something like this:

    ClassMethod foo(this, propName As %String)
    {
      set $property(this, propName)=$property(this, propName) * 2
    }
    
    Method test()
    {
      set ..intA=5
      do ##class(b).foo(%this, "intA")
    }