Using InterSystems Development Environments — Atelier and Studio
Adding Methods to a Class
This chapter discusses how to add and edit method definitions in a class definition.
The operations that are associated with an object and can be performed by it are referred to as methods. Every class definition may contain zero or more methods.
You can add a new method to a class definition in two ways:
Adding a method to the class definition using the Class Editor.
Using the New Method wizard
To add a method using the Class Editor, position the cursor on a blank line in the Class Editor and enter a method declaration:
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.
You can invoke the New Method wizard using the
. Alternatively, right-click the Class Inspector and select
or select the New Method
from the toolbar.
The New Method wizard prompts you for information. Select Finish
at any time (default values are provided for any information you have not specified).
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.
(optional) Description of the new method. This description is used when the class' documentation is displayed in the online class library documentation.
Every method has a signature that indicates its return type (if any) as well as its formal argument list (if any). For method signature you may specify the following:
(optional) Indicates the type of the value returned by this method. This type is the name of a Caché class. You can type this name in directly or choose from a list of available classes using the Browse
For example, a method that returns a true (1) or false (0) value would have a return type of %Boolean
. Leave this field empty if your new method has no return value.
(optional) Indicates the names, types, default values, and how data is passed (by reference or by value) for any formal arguments. The arguments are displayed in order in a table. You can add a new item to the argument list using Add
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.
You may specify additional characteristics for your method. These include:
(optional) Indicates whether this method is public or private. Private methods may 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.
(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.
Indicates the language used to create the method.
If you want, you may enter the implementation (code) for the new method by typing lines of source code into the text editor window. You can also enter this source code after running the wizard using the Class Editor.
After running the New Method wizard the Class Editor window 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
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.
simplifies the process of overriding a class item, such as a specific method by displaying a list of all the methods defined by superclasses that can be overridden by the current class.
To do this, follow these steps:
Open (or create) a persistent class definition in Studio.
and select the Methods tab. This displays a dialog window containing a list of methods which you can override.
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.