Skip to main content

Casting a Method

Casting a Method

To cast a method of one class as a method of another class, the syntax is either of the following:

>Do ##class(Package.Class1)Class2Instance.Method(Args)
>Set localname = ##class(Package.Class1)Class2Instance.Method(Args)

You can cast both class methods and instance methods.

For example, suppose that two classes, MyClass.Up and MyClass.Down, both have Go() methods. For MyClass.Up, this method is as follows

Method Go()
{
    Write "Go up.",!
}

For MyClass.Down, the Go() method is as follows:

Method Go()
{
    Write "Go down.",!
}

You can then create an instance of MyClass.Up and use it to invoke the MyClass.Down.Go method:

>Set LocalInstance = ##class(MyClass.Up).%New()
 
>Do ##class(MyClass.Down)LocalInstance.Go()
Go down.

It is also valid to use ##class as part of an expression, as in

 Write ##class(Class).Method(args)*2

without setting a variable equal to the return value.

A more generic way to refer to other methods are the $METHOD and $CLASSMETHOD functions, which are for instance and class methods, respectively; these are described in Dynamically Accessing Objects. These provide a mechanism for referring to packages, classes, and methods programmatically.

FeedbackOpens in a new tab