Supports calls to an instance method.
Description
$METHOD executes a named instance method for a specified instance of a designated class.
This function permits an ObjectScript program to call an arbitrary method in an existing instance of some class. Since the first argument must be a reference to an object, it is computed at execution time. The method name may be computed at runtime or supplied as a string literal. If the method takes arguments, they are supplied from the list of arguments that follow the method name. A maximum of 255 argument values may be passed to the method. If the method requires arguments, the rules that govern what must be supplied are those of the target method. To invoke a class method rather than an instance method, use the $CLASSMETHOD function.
The invocation of $METHOD as a function or a procedure determines the invocation of the target method. You can invoke $METHOD using the DO command, discarding the return value. Like all DO command arguments, $METHOD can take a postconditional parameter when called by DO.
When used within one method of a class instance to refer to another method of that instance, the $METHOD may omit instance. The comma that would normally follow Instance is still required, however.
If there is an attempt to invoke a method that is nonexistent or that is declared to be a class method, this results in a <METHOD DOES NOT EXIST> error.
Example
The following example shows $METHOD used as a function:
SET ListOfStuff = ##class(%Library.ListOfDataTypes).%New()
FOR i = "First", "Second", "Third", "Fourth"
{
DO ListOfStuff.Insert((i _ "-Element"))
}
SET methodname = "Count"
SET elements = $METHOD(ListOfStuff,methodname)
WRITE "Elements: ",elements,!
SET i = $RANDOM(elements) + 1
WRITE "Element #", i , " = " , $METHOD(ListOfStuff,"GetAt", i), !