Skip to main content
Previous section   Next section

Calling ObjectScript Methods and Functions

This chapter describes methods that call class methods and functions from ObjectScript classes and routines. They allow .NET applications to call your custom ObjectScript classmethods or functions as easily as they call native .NET methods. See the following sections for details and examples:
  • Class Method Calls — demonstrates how to call user defined ObjectScript class methods.
  • Function Calls — demonstrates how to call user defined ObjectScript functions and procedures.
Note:
In some cases, these Native API methods can also be used with InterSystems classes defined in the Class Library. For example, this call returns some system information:
  String currentNameSpace = iris.ClassMethodString("%SYSTEM.SYS","NameSpace");
Unfortunately, most class library methods return only a status code, passing the actual results back in arguments (which cannot be accessed by the Native API). Best practice is always to call class library methods indirectly, from within a user defined wrapper class. This allows you to use the full power of the class library, and also produces more maintainable code.

Class Method Calls

The Native API methods in this section call user defined ObjectScript class methods, and return values of the type indicated by the method name: ClassMethodBool(), ClassMethodBytes(), ClassMethodDouble(), ClassMethodLong(), ClassMethodString(), or ClassMethodVoid() (no return value).
They take String arguments for className and methodName, plus
0
or more method arguments. Trailing arguments may be omitted in argument lists, either by passing fewer than the full number of arguments, or by passing
null
for trailing arguments. An exception will be thrown if a non-null argument is passed to the right of a null argument.
Calling ObjectScript class methods with the Native API
The code in this example calls class methods of each supported datatype from ObjectScript test class User.NativeTest (listed immediately after the example). Assume that iris is an existing instance of class IRIS, and is currently connected to the server.
  String className = "User.NativeTest";

  bool boolval = IRIS.ClassMethodBool(className,"cmBoolean",7,7);
  Console.WriteLine("cmBoolean() finds if two numbers are equal (true=1,false=0): ",boolval);

  cmValue = new String(IRIS.ClassMethodBytes(className,"cmBytes","byteArray"));
  Console.WriteLine(className +".cmBytes() returned value: " + cmValue);

  cmValue = IRIS.ClassMethodString(className,"cmString","Java Test");
  Console.WriteLine(className +".cmString() returned value: " + cmValue);

  cmValue = IRIS.ClassMethodLong(className,"cmLong",7,8);
  Console.WriteLine (className + ".cmLong() returned value: " + cmValue);

  cmValue = IRIS.ClassMethodDouble(className,"cmDouble",7.56);
  Console.WriteLine (className + ".cmDouble() returned value: " + cmValue);

  try {
    IRIS.ClassMethodVoid(className,"cmVoid",67);
    cmValue = iris.Get("cm1");
  } catch (Exception e){
    Console.WriteLine("method failed");
  }
  Console.WriteLine (className + ".cmVoid() set global array cm1 to value: " + cmValue);
// Get an error message for a failed method call
  try {
    System.out.println("Calling nonexistent class method \"notLegal\"... ");
    iris.classMethodStatusCode(className,"notLegal");
  } catch (RuntimeException e) {
    System.out.println("Call to class method \"notLegal\" returned error:");
    System.out.println(e.getMessage());
  }
ObjectScript Class User.NativeTest
To run the previous example, this ObjectScript class must be compiled and available on the server:
Class User.NativeTest Extends %Persistent
{
  ClassMethod cmBoolean(cm1 As %Integer,cm2 As %Integer) As %Boolean
  {
   quit (cm1=cm2)
  }
  ClassMethod cmBytes(cm1 As %Integer,cm2 As %Integer, cm3 As %Integer) As %Binary
  {
    quit $CHAR(cm1,cm2,cm3)
  }
  ClassMethod cmString(cm1 As %String) As %String
  {
   quit "Hello "_cm1
  }
  ClassMethod cmLong(cm1 As %Integer, cm2 As %Integer) As %Integer
  {
   quit cm1+cm2
  }
  ClassMethod cmDouble(cm1 As %Double) As %Double
  {
   quit cm1 * 100
  }
  ClassMethod cmVoid(cm1 As %Integer)
  {
   set ^cmGlobal=cm1
   quit
  }
}

Function Calls

The Native API methods in this section call user-defined ObjectScript functions or procedures (see “Callable User-defined Code Modules” in Using ObjectScript), and return a value of the type indicated by the method name: FunctionBool(), FunctionBytes(), FunctionDouble(), FunctionLong(), FunctionString(), or Procedure() (no return value).
They take String arguments for functionName and routineName, plus
0
or more function arguments. Trailing arguments may be omitted in argument lists, either by passing fewer than the full number of arguments, or by passing
null
for trailing arguments. An exception will be thrown if a non-null argument is passed to the right of a null argument.
Note:
These methods are designed to call functions in user-defined routines. ObjectScript system functions (see “ObjectScript Functions” in the ObjectScript Reference) cannot be called directly from your Java code. However, you can write an ObjectScript wrapper function to call a system function indirectly. For instance, the sample code later in this section includes user-defined function FunctionBytes(), which returns the value of a call to ObjectScript system function $CHAR.
Calling functions of ObjectScript routines with the Native API
The code in this example calls functions of each supported datatype from ObjectScript routine NativeRoutine (File NativeRoutine.mac, listed immediately after this example). Assume that iris is an existing instance of class IRIS, and is currently connected to the server.
  String routineName = "NativeRoutine";
  Object fnValue = "";

  fnValue = IRIS.FunctionBool("funcBoolean",routineName,false);
  Console.WriteLine(routineName + ".funcBoolean() returned value: " + fnValue);

  fnValue = new String(IRIS.FunctionBytes ("funcBytes",routineName,"byteArray"));
  Console.WriteLine(routineName + ".funcBytes() returned value: " + fnValue);

  fnValue = IRIS.FunctionString ("funcString",routineName,"Test String");
  Console.WriteLine(routineName + ".funcString() returned value: " + fnValue);

  fnValue = IRIS.FunctionInt ("funcLong",routineName,7,8);
  Console.WriteLine(routineName + ".funcLong() returned value: " + fnValue);

  fnValue = IRIS.FunctionDouble ("funcDouble",routineName,7.56);
  Console.WriteLine(routineName + ".funcDouble() returned value: " + fnValue);

  try {
    IRIS.Procedure("funcProcedure",routineName,67);
  } catch (Exception e){
    fnValue = "procedure failed.";
  }
  Console.Write(routineName + ".funcVoid() set global array fn1 to value: " + fnValue);
ObjectScript Routine NativeRoutine.mac
To run the previous example, this ObjectScript routine must be compiled and available on the server:
funcBoolean(fn1,fn2) public {
   quit (fn1=fn2)
}
funcBytes(fn1,fn2,fn3) public {
    quit $CHAR(fn1,fn2,fn3)
}
funcString(fn1) public {
    quit "Hello "_fn1
}
funcLong(fn1,fn2) public {
    quit fn1+fn2
}
funcDouble(fn1) public {
    quit fn1 * 100
}
funcProcedure(fn1) public {
    set ^funcGlobal=fn1
    quit
}
Previous section   Next section