Adding Methods to a Class
This topic discusses how to add and edit method definitions in a class definition.
You can add a new method to a class definition in two ways:
To add a method using the Class Editor, position the cursor on a blank line in the Class Editor and enter a method declaration such as:
Class MyApp.Person Extends %Persistent
Method NewMethod() As %String
Alternatively, you can do this by copying and pasting an existing method declaration and then editing it.
For details on method definitions, see Defining and Calling Methods in Defining and Using Classes. Also see Method Definitions in the Class Definition Reference.
New Method Wizard
You can invoke the New Method wizard by selecting Class > Add > Method. Alternatively, right-click the Class Inspector and select Add > New Method. You can also click the New Method button from the toolbar.
The New Method wizard prompts you for information. Click Finish at any time (default values are provided for any information you have not specified).
Name and Description Page
The New Method wizard prompts you for the following information (you can later modify any of these values):
(required) Name of the new method. This name must be a valid method name and must not conflict with the name of a previously defined method.
See Rules and Guidelines for Identifiers.
(optional) Description of the new method. This description is used when the class' documentation is displayed in the online class library documentation.
A description may include HTML formatting tags. See Creating Class Documentation in Defining and Using Classes.
Method Signature Page
Every method has a signature that indicates its return type (if any) as well as its formal argument list (if any). For a method signature you may specify the following:
(optional) Indicates the type of the value returned by this method. This type is the name of an InterSystems IRIS® class. You can type this name in directly or choose from a list of available classes using the Browse button.
For example, a method that returns a true (1) or false (0) value would have a return type of %BooleanOpens in a new tab. Leave this field empty if your new method has no return value.
(optional) Indicates the names, types and default values of the method's formal arguments along with how data is passed (by reference or by value). The arguments are displayed in order in a table. You can add a new item to the argument list by clicking the Add button located on the side of the table. This displays a popup dialog allowing you to specify the name of the argument, its type, its optional default value, and whether it is passed by value or by reference. Using the other buttons, you can remove and rearrange the order of items in the list.
Method Characteristics Page
You may specify additional characteristics for your method. These include:
(optional) Indicates whether this method is public or private. Private methods can only be invoked from other methods of the same class.
(optional) Indicates whether this method is final. Final methods cannot be overridden by subclasses.
(optional) Indicates that the new method is a class method (as opposed to an instance method). Class methods may be invoked without having an object instance.
SQL Stored Procedure
(optional) Indicates that this method is accessible to an ODBC or JDBC client as a stored procedure. Only class methods may be projected as SQL Stored Procedures.
If you want, you may enter the implementation (code) for the new method by typing lines of source code into the Class Editor. You can also enter this source code after running the wizard.
Results of Running the New Method Wizard
After running the New Method wizard, the Class Editor is updated to include the new method definition. You can edit it using either the Class Editor or the Class Inspector. For example:
/// This is a Person class
class MyApp.Person extends %Persistent
Method Print() As %Boolean
Overriding a Method
The Refactor submenu is available only when Studio is connected to a Windows server. The Override menu item is available in other platforms.
One of the powerful features of object-based development is that classes inherit methods from their superclasses. In some cases, you may want to override, that is, provide a new implementation for, a method inherited from a superclass.
Class > Refactor > Override simplifies the process of overriding a class method by displaying a list of all the methods defined by superclasses that can be overridden by the current class.
For example, in a persistent class, you may want to override the default implementation of the %OnValidateObject method provided by the %RegisteredObjectOpens in a new tab class in order to specify custom validation that occurs when an instance of your class is saved.
To do this, follow these steps:
Open (or create) a persistent class definition in Studio.
Select Class > Refactor > Override and select the Methods tab. This displays a dialog window containing a list of methods which you can override.
Select %OnValidateObject from the list and select OK button.
Your class definition now includes a definition for an %OnValidateObject method:
class MyApp.Person extends %Persistent
Method %OnValidateObject() As %Status
At this point, you can use the Class Editor to add code to the body of the method.