Caché ObjectScript Reference
[Back] [Next]
Go to:

Supports calls to an instance method.
$METHOD(instance, methodname, arg1, arg2, arg3, ... )
instance An expression that evaluates to an object reference. The value of the expression must be that of an in-memory instance of a class.
methodname An expression that evaluates to a string. The value of the string must exactly match the name of an existing method in the instance of the class given as the first argument.
arg1, arg2, arg3, ... A series of expressions to be substituted for the arguments to the designated method. The values of the expressions can be of any type. It is the responsibility of the implementer to make sure that the supplied expressions both match in type and have values with the bounds that the method expects. (If the specified method expects no arguments then nothing beyond classname and methodname need be used in the function invocation. If the method requires arguments, the rules that govern what must be supplied are those of the target method.)
$METHOD executes a named instance method for a specified instance of a designated class.
This function permits a Caché 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.
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.
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), !
See Also