Skip to main content

$CLASSMETHOD (ObjectScript)

Executes a named class method in the designated class.

Synopsis

$CLASSMETHOD(classname, methodname, arg1, arg2, arg3, ... )

Arguments

Argument Description
classname

Optional — An expression that evaluates to a string. The content of the string must match exactly the name of an existing, accessible, previously compiled class. In the case of references to InterSystems IRIS classes, the name may be either in its canonical form (%Library.StringOpens in a new tab), or its abbreviated form (%StringOpens in a new tab).

If classname is omitted, the current class context is used. (You can use $THIS to determine the current class context.) Note that when classname is omitted the placeholder comma must be specified.

methodname An expression which evaluates to a string. The value of the string must match the name of an existing class method in the class identified by classname.
arg1, arg2, arg3, ... Optional — A series of expressions to be substituted sequentially for the arguments to the designated method. The values of the expressions can be of any type. It is the responsibility of the implementor to make sure that the type of the supplied expressions match what the method expects, and have values within the bounds declared. (If the specified method expects no arguments then no arguments beyond the methodname need be given in the function invocation. If the method requires arguments, the rules that govern what must be supplied are those of the target method.)

Description

$CLASSMETHOD permits an ObjectScript program to invoke an arbitrary class method in an arbitrary class. Both the class name and the method name may be computed at runtime or supplied as string constants. To invoke an instance method rather than a class method, use the $METHOD function.

If the method takes arguments, they are supplied by the list of arguments that follow the method name. A maximum of 255 argument values may be passed to the method.

The invocation of $CLASSMETHOD as a function or a procedure determines the invocation of the target method. You can invoke $CLASSMETHOD using the JOB command or the DO command, discarding the return value. Like all DO command arguments, $CLASSMETHOD can take a postconditional parameter when called by DO.

An attempt to invoke a nonexistent class results in a <CLASS DOES NOT EXIST> error, followed by the current namespace name and the specified class name. For example, attempting to invoke the nonexistent classname “Fred” results in the error <CLASS DOES NOT EXIST> *User.Fred. Specifying the empty string for classname results in <CLASS DOES NOT EXIST> *(No name).

An attempt to invoke a nonexistent class method results in a <METHOD DOES NOT EXIST> error.

Examples

The following example shows $CLASSMETHOD used as a function:

 SET classname = "%Dictionary.ClassDefinition"
 SET classmethodname = "NormalizeClassname"
 SET singleargument = "%String"
 WRITE $CLASSMETHOD(classname,classmethodname,singleargument),!

It returns %Library.String.

The following example shows $CLASSMETHOD with two arguments:

 WRITE $CLASSMETHOD("%Library.Persistent","%PackageName"),!
 WRITE $CLASSMETHOD("%Library.Persistent","%ClassName")

These calls return %Library and %Persistent.

The following example uses $CLASSMETHOD to execute a Dynamic SQL query:

  SET q1="SELECT Age,Name FROM Sample.Person "
  SET q2="WHERE Age > ? AND Age < ? "
  SET q3="ORDER by Age"
  SET myquery=q1_q2_q3
  SET rset=$CLASSMETHOD("%SQL.Statement","%ExecDirect",,myquery,12,20)
  DO rset.%Display()
  WRITE !,"Teenagers in Sample.Person"

See Also

FeedbackOpens in a new tab