docs.intersystems.com
Home / Using the InterSystems Native API for .NET / Calling ObjectScript Methods and Functions

Using the InterSystems Native API for .NET
Calling ObjectScript Methods and Functions
Previous section           Next section
InterSystems: The power behind what matters   
Search:  


This chapter describes a set of IRIS class methods that allow an application to call ObjectScript class methods and functions from the InterSystems IRIS™ class library.
Class Method Calls
The following Native API methods call a specified ObjectScript class method. They take String arguments for className and methodName, plus an Object array containing 0 or more method arguments. The result is returned as the indicated type:
Trailing arguments may be omitted in argument lists, causing default values to be used for those arguments, 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.
The code in this example calls class methods of each datatype from an ObjectScript test class named User.NativeTest (see ObjectScript Class User.NativeTest in the following section for details).
Calling class methods from ObjectScript class User.NativeTest
  String className = "User.NativeTest";

  Console.Write("Calling cosBoolean class method. Result: ");
  Console.WriteLine(IRIS.ClassMethodBool(className,"cosBoolean",false));

  Console.Write("Calling cosBytes class method. Result: ");
  Console.WriteLine(new String(IRIS.ClassMethodBytes(className,"cosBytes","byteArray")));

  Console.Write("Calling cosString class method. Result: ");
  Console.WriteLine(IRIS.ClassMethodString(className,"cosString","Java Test"));

  Console.Write("Calling cosLong class method. Result: ");
  Console.WriteLine(IRIS.ClassMethodLong(className,"cosLong",7,8));

  Console.Write("Calling cosDouble class method. Result: ");
  Console.WriteLine(IRIS.ClassMethodDouble(className,"cosDouble",7.56));

  Console.Write("Calling cosVoid class method. Result: ");
  IRIS.ClassMethodVoid(className,"cosVoid",67);
  Console.WriteLine("(method returned successfully)");
Function Calls
The following methods call user-defined ObjectScript functions or procedures. They take String arguments for functionName and routineName, plus an Object array containing 0 or more function arguments. The result is returned as the indicated type:
Trailing arguments may be omitted in argument lists, causing default values to be used for those arguments, 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.
The code in this example calls functions of each datatype from an ObjectScript test routine named NativeRoutine (File NativeRoutine.mac. See ObjectScript Routine NativeRoutine.mac in the following section for details).
Calling functions from NativeRoutine.mac
  String routineName = "NativeRoutine";

  Console.Write("Calling funcBoolean function. Result: ");
  Console.WriteLine(IRIS.FunctionBool("funcBoolean",routineName,false));

  Console.Write("Calling funcBytes function. Result: ");
  Console.WriteLine(new String(IRIS.FunctionBytes ("funcBytes",routineName,"byteArray")));

  Console.Write("Calling funcString function. Result: ");
  Console.WriteLine(IRIS.FunctionString ("funcString",routineName,"Test String"));

  Console.Write("Calling funcLong function. Result: ");
  Console.WriteLine(IRIS.FunctionInt ("funcLong",routineName,7,8));

  Console.Write("Calling funcDouble function. Result: ");
  Console.WriteLine(IRIS.FunctionDouble ("funcDouble",routineName,7.56));

  Console.Write("Calling funcProcedure class method. Result: ");
  IRIS.Procedure("funcProcedure",routineName,67);
  Console.WriteLine("(function funcProcedure returned successfully)");
Sample ObjectScript Methods and Functions
To run the examples, these ObjectScript class methods and functions must be compiled and available on the server:
ObjectScript Class User.NativeTest
Class User.NativeTest Extends %Persistent
{
  ClassMethod cosBoolean(p1 As %Boolean) As %Boolean
  {
   Quit 0
  }
  ClassMethod cosBytes(p1 As %String) As %Binary
  {
   Quit $C(65,66,67,68,69,70,71,72,73,74)
  }
  ClassMethod cosString(p1 As %String) As %String
  {
   Quit "Hello "_p1
  }
  ClassMethod cosLong(p1 As %Integer, p2 As %Integer) As %Integer
  {
   Quit p1+p2
  }
  ClassMethod cosDouble(p1 As %Double) As %Double
  {
   Quit p1 * 100
  }
  ClassMethod cosVoid(p1 As %Integer)
  {
   Set ^p1=p1
   Quit
  }
}
ObjectScript Routine NativeRoutine.mac
funcBoolean(p1) public {
    Quit 0
}
funcBytes(p1) public {
    Quit $C(65,66,67,68,69,70,71,72,73,74)
}
funcString(p1) public {
    Quit "Hello "_p1
}
funcLong(p1,p2) public {
    Quit p1+p2
}
funcDouble(p1) public {
    Quit p1 * 100
}
funcProcedure(p1) public {
    Set ^p1=p1
    Quit
}


Previous section           Next section
Send us comments on this page
View this book as PDF   |  Download all PDFs
Copyright © 1997-2019 InterSystems Corporation, Cambridge, MA
Content Date/Time: 2019-07-19 06:48:22