Language > Objects > Creating Object Classes > Creating Custom Object Method Routines

# Creating Custom Object Method Routines

IDL objects can have associated procedures and functions called methods. Methods are called on objects via their object references using the method invocation operator.

While object methods are constructed in the same way as any other IDL procedure or function, they are different from other routines in the following ways:

- Object methods are defined using a special naming convention that incorporates the name of the class to which the method belongs (see Defining Method Routines).
- All method routines automatically pass an implicit argument named Self, which contains the object reference of the object on which the method is called.
- Object methods cannot be called on their own. You must use the method invocation operator and supply a valid object reference, either of the class the method belongs to or of one of that class’ subclasses.

## Defining Method Routines

Method routines are defined in the same way as other IDL procedures and functions, with the exception that the name of the class to which they belong, along with two colons, is prepended to the method name:

PRO ClassName::Method

IDL statements

END

or

FUNCTION ClassName::Method, Argument1

IDL statements

RETURN, value

END

For example, suppose we create two objects, each with its own “print” method.

First, define two class structures:

struct = { class1, data1:0.0 }

struct = { class2, data2a:0, data2b:0L, INHERITS class1 }

Now we define two “print” methods to print the contents of any objects of either of these two classes. (If you are typing this at the IDL command line, enter the .RUN command before each of the following procedure definitions.)

PRO class1::Print1

PRINT, self.data1

END

PRO class2::Print2

PRINT, self.data1

PRINT, self.data2a, self.data2b

END

Once these procedures are defined, any objects of class1 have access to the method Print1, and any objects of class2 have access to both Print1 and Print2 (because class2 is a subclass of—it inherits from—class1). Note that the Print2 method prints the data1 field inherited from class1.

Note: It is not necessary to give different method names to methods from different classes, as we have done here with Print1 and Print2. In fact, in most cases both methods would have simply been called Print, with each object class knowing only about its own version of the method.

## The Implicit Self Argument

Every method routine has an implicit argument parameter named Self. The Self parameter always contains the object reference of the object on which the method is called. In the method routines created above, Self is used to specify which object the data fields should be printed from using the structure dot operator:

PRINT, self.data1

You do not need to explicitly pass the Self argument; in fact, if you try to specify an argument called Self when defining a method routine, IDL will issue an error.

## Calling Method Routines

IDL recognizes two forms of the method invocation operator:

Form | Syntax | Notes
. | Obj.Method_Name | Introduced in IDL 8.0
-> | Obj->Method_Name | Introduced in IDL 5.0

Beginning with IDL 8.0, you can use the . and -> forms of the operator interchangeably; they are equivalent. Code intended to run on systems hosting versions of IDL prior to 8.0 should use the -> form.

Note: Discussion and example code in the IDL help system uses both forms of the method invocation operator. Discussion of object-oriented programming generally uses the older -> form, while discussion of the IDL graphics API introduced in IDL 8.0 generally uses the . form.

where Obj is an object reference and Method is a method belonging either to the object’s class or to one of its superclasses. Method may be specified either partially (using only the method name) or completely using both the class name and method name, connected with two colons:

Obj.Class::Method

See Specifying Class Names in Method Calls for more information.

The exact method syntax is slightly different from other routine invocations:

; For a procedure method.

ObjRef.Method

; For a function method.

Result = ObjRef.Method()

Where ObjRef is an object reference belonging to the same class as the Method, or to one of that class’ subclasses. We can illustrate this behavior using the Print1 and Print2 methods defined above.

First, define two new objects:

A = class1([args])

B = class2([args])

or

A = OBJ_NEW('class1')

B = OBJ_NEW('class2')

We can call Print1 on the object A as follows:

A.Print1

IDL prints:

0.00000

Similarly, we can call Print2 on the object B:

B->Print2

IDL prints:

0.00000

0 0

Since the object B inherits its properties from class1, we can also call Print1 on the object B:

B.Print1

IDL prints:

0.00000

We cannot, however, call Print2 on the object A, since class1 does not inherit the properties of class2:

A.Print2

IDL prints:

% Attempt to call undefined method: 'CLASS1::PRINT2'.

## Searching for Method Routines

When a method is called on an object reference, IDL searches for it as with any procedure or function, and calls it if it can be found, following the naming convention established for structure definition routines (see Automatic Class Structure Definition). In other words, IDL discovers methods as it needs them in the same way as regular procedures and functions, with the exception that it searches for files named

classname__method.pro

rather than simply

method.pro

Remember that there are two underscores in the file name, and two colons in the method routine’s name.