Release 10.1A: OpenEdge Getting Started:
Object-oriented Programming

> ## Defining an object reference as a return type

> User-defined functions and class methods can define classes as return types. Classes are always returned by value. However, a copy of the object reference is made, not the object itself. An object reference cannot be returned to or from a remote application server.

> This is the syntax to define a class return type for a method:

> Syntax
>     METHOD { PUBLIC | PROTECTED | PRIVATE } [ OVERRIDE ] [ FINAL] [ CLASS ]
>         type-name method-name ( [ parameter [ , parameter ] ... ] ) :

> This is the syntax to define a class return type for a user-defined function:

> Syntax
>     FUNCTION function-name RETURNS [ CLASS ] type-name
>         ( [ parameter [ , parameter ] ... ] ) .

> Element descriptions for these syntax diagrams follow:

> `CLASS`

> The `CLASS` keyword is required if type-name conflicts with an abbreviation for a built in Progress data type, such as `INT` (`INTEGER`). Otherwise, it can optionally be used to clarify the readability of the statement.

> ``type-name``

> The type name of the class or interface being returned. For more information on type names, see the "Using the CLASS construct" section or the "Using the INTERFACE construct" section, respectively.

> ``method-name``

> The name of the method.

> `[ `parameter` [ , `parameter` ] ... ]`

> Any parameters that you define for the method or user-defined function. For more information on the syntax of `parameter`, see the “Parameter definition syntax” reference entry in OpenEdge Development: Progress 4GL Reference .

> ``function-name``

> The name of the user-defined function.

> The following code fragment, from the sample class `acme.myObjs.CustObj`, illustrates a method returning a class return type, in this case, to initialize an error object (`acme.myObjs.Common.ErrorObj` class) for use by the current instance of `acme.myObjs.CustObj`:

>     CLASS acme.myObjs.CustObj INHERITS acme.myObjs.Common.CommonObj
>                               IMPLEMENTS acme.myObjs.Interfaces.IBusObj:
>         ...
>         DEFINE PRIVATE VARIABLE rError
>             AS CLASS acme.myObjs.Common.ErrorObj NO-UNDO.
>         CONSTRUCTOR PUBLIC CustObj( ):
>             iNumCusts = 0.
>             /* Fill temp table and get row count */
>             FOR EACH Customer WHERE CustNum < 15:
>                 CREATE ttCust.
>                 ASSIGN
>                     ttCust.CustNum = Customer.CustNum
>                     ttCust.Name = Customer.Name
>                     iNumCusts = iNumCusts + 1.
>             END.
>             rError = ErrorHandler("acme.MyObjs.CustObj").
>         END CONSTRUCTOR.
>         ...
>     END CLASS.

> The `ErrorHandler( )` method definition is defined in the immediate super class. This method instantiates the `acme.myObjs.Common.ErrorObj` class and returns the object to the caller, as shown:

>     CLASS acme.myObjs.Common.CommonObj:
>         ...
>         METHOD PROTECTED CLASS acme.myObjs.Common.ErrorObj ErrorHandler
>                 (INPUT iObjType AS CHARACTER):
>             DEFINE VARIABLE rError AS CLASS acme.myObjs.Common.ErrorObj NO-UNDO.
>             rError = NEW acme.myObjs.Common.ErrorObj (INPUT iObjType).
>             RETURN rError.
>         END METHOD.
>     END CLASS.

> Progress treats a return from a class method the same as an assignment. That is, the compiler verifies that the object reference being returned by the method is consistent with the variable it is being assigned to. For more information on the compatibility rules for assigning object references, see the "Assigning object references" section.

> Thus, the rules for assignment of the return from a method or user-defined function are the same as an `OUTPUT` parameter from a method. (For more information, see the "Defining an object reference as a parameter" section). The caller of the method must define an object reference that is the same class as the return object, or a super class or interface of the returned object reference. As with parameters, if the caller has as its target variable an object reference to a super class or interface, the caller can only invoke those methods that are defined by the super class or interface. If the caller attempts to invoke a method that is not defined in the super class or interface, the compiler generates an error.

Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095