﻿ how run main method | InterSystems Developer Community | ObjectScript|Caché
yeung elijah · Jan 23

how run main method

Hi, I am a novice.
how run main method by stdio -> objectscript?

Class test.TestClass Extends %Persistent

// main

Method Main()
//how run main method


Product version: IRIS 2020.4
1 0 3 120
Log in or sign up to continue

from terminal prompt:

do ##class(test.TestClass).Main()

Thank you. Can't it run in the development tool？


You can run the method in the 'Output' window which you can access from the 'View' menu.

Just a note on the difference between Cache Classes and Classes in other OO based languages. 

Cache supports two types of method:

Method ABC(Param1, param2, ... paramN)


ClassMethod XYZ(param1, param2, ... ParamN)

a Method acts on a instance of your class whereas a class method is independent of the object instance. i.e. you do not need to create a new instance or open an existing instance of a class in order to invoke a class method.

The second thing is that there a developer hooks that allow you to run code based on the following actions:

%New(): If you write a method %OnNew() this method will be run after the %New() method has been run. It allows you to do any initiation logic on the new instance that has been created.

%OnOpen(): This method is called after the instance of the class has been opened.

%Save(): There are two methods %OnBeforeSave and %OnAfterSave() that you can write that are called before or after the %Save() method.

%Delete: Likewise has two methods %OnBeforeDelete () and %OnAfterDelete() where you can write code that is executed before or after the %Delete() Method.

We don't really have the concept of a "Main" method that is automatically invoked when you reference the class other than through the %OnNew() and %OnOpen() methods.

In Ensemble things are slightly different in that Business Service/Business Process and Business Operation classes have methods such as OnProcessInput(), OnProcess() and in the case of Business Operations there is an XDATA block where you can specify the method to be invoked based on the message type being processed by the Business Operation.  You could consider these methods as the 'Main' methods of the class but in reality they are the methods where the developer is expected to put the application logic for the class. There are other methods that the Ensemble Production will execute before it is ready to run your code and there are methods that the Ensemble Production will run after your methods have completed.

Cache/Ensemble/IRIS also support the ability to write application/system specific code that will be executed when Cache Starts Up or Shuts Down and likewise when an Ensemble Production Starts Up or Shuts Down.

Have a look at the documentation on %ZSTART for more information on what you can do when Cache starts up or shuts down. There is similar documentation in relation to Ensemble Productions.