Skip to main content

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
{
    Quit ""
}
}

Alternatively, you can do this by copying and pasting an existing method declaration and then editing it.

For details on method definitions, see “Class Definitions” in the reference “Class Definitions” in Caché Class Definition Reference.

New Method Wizard

You can invoke the New Method wizard using the Class > Add > Method. Alternatively, right-click the Class Inspector and select Add Method or select the New Method icon generated description: newmethodicon 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).

Name and Description Page

The New Method wizard prompts you for the following information (you can later modify any of these values):

Method Name

(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.

For a general discussion on names see the chapter “Caché Classes ” in Using Caché Objects.

Description

(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. For more details, see “Using HTML Markup in Class Documentation” in the chapter “Defining and Compiling Classes” in Using Caché Objects.

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 method signature you may specify the following:

Return Type

(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 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.

For a description of the basic data type classes provided with Caché see the chapter “Data Types” in Using Caché Objects.

Arguments

(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.

Method Characteristics Page

You may specify additional characteristics for your method. These include:

Private

(optional) Indicates whether this method is public or private. Private methods may only be invoked from other methods of the same class.

Final

(optional) Indicates whether this method is final. Final methods cannot be overridden by subclasses.

Class Method

(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.

Language

Indicates the language used to create the method.

Implementation Page

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.

Results of Running the New Method Wizard

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
{
    Write "Hello"
    Quit 1
}
}

Overriding a Method

Note:

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 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.

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:

  1. Open (or create) a persistent class definition in Studio.

  2. Select Class > Refactor > Override and select the Methods tab. This displays a dialog window containing a list of methods which you can override.

  3. 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.

FeedbackOpens in a new tab