Try OpenEdge Now
skip to main content
Object-oriented Programming
Programming with Class-based Objects : Assigning object references : Using the CAST function
 

Using the CAST function

You can thus use the CAST function wherever an object reference is permitted:
*To assign an object reference to an object reference data element, especially when assigning a super class or interface reference to a related subclass or interface-implementing class element, respectively.
*To pass a super class object reference as an argument to a routine INPUT parameter defined as a subclass type, or to pass an interface object reference as an argument to a routine INPUT parameter defined as a class that implements the passed interface type.
*To access a method, property, or data member on a subclass of a specified class-type object reference, or on a class that implements a specified interface-type object reference. You can do this by implicitly using the return value of the CAST function as the new object reference on which to access the method, property, or data member.
Syntax descriptions for each of these uses of the CAST function follow.
This is the syntax for the CAST function when used to assign an object reference to another object reference data element:

Syntax

target-object-reference = CAST ( object-reference , target-type-name )
This is the syntax for the CAST function when used to pass an object reference to an INPUT parameter:
routine-name ( [ INPUT ] CAST ( object-reference , target-type-name ) )
This is the syntax for the CAST function when used to cast an object reference to access a method, property, or data member:
CAST ( object-reference , target-type-name ) : member-name[ ( ... ) ]
Element descriptions for these syntax diagrams follow:
target-object-reference
A data element defined as the object reference type specified by target-type-name, typically defined to reference one of the following types:
*A subclass of the class whose type defines the object-reference
*A class that implements the interface whose type defines the object-reference
object-reference
An object reference source, whose type is typically defined as a super class of target-type-name, or an interface implemented by a class type specified by target-type-name. This can be a variable or it can be a temp-table field defined as Progress.Lang.Object. At run time, the AVM verifies that object-reference in fact points to an instance of the specified target-type-name.
target-type-name
The type name of the target class or interface type for the cast. Any class type name must specify one of the following types:
*The same class whose type defines object-reference
*A super class of the class whose type defines object-reference
*A subclass of the class whose type defines object-reference
*A class that implements the interface whose type defines object-reference
Any interface type name must specify one of the following types:
*The same interface whose type defines object-reference
*An interface implemented by the class type that defines object-reference
The specified type name can be a fully qualified object type name or an unqualified class or interface name, depending on the presence of an appropriate USING statement in the class or procedure file. For more information on object type names, see Defining and referencing object type names. For more information on the USING statement, see Referencing an object type name without its package.
routine-name
The name of any procedure, user-defined function, or method that takes an object reference argument as INPUT. The corresponding INPUT parameter defined by routine-name must have a data type that is compatible with target-type-name.
member-name [ ( ... ) ]
This can be one of the following:
*The name of a PUBLIC method (and its parameter list) defined by the class or interface type specified by target-type-name. The compiler verifies that member-name specifies a valid method defined by target-type-name and that the parameters ((...)) are valid.
*The name of a PUBLIC data member defined by the class type specified by target-type-name. The compiler verifies that member-name specifies a valid data member defined by target-type-name.
*The name of an accessible PUBLIC property defined by the class type specified by target-type-name. The compiler verifies that member-name specifies a valid property defined by target-type-name, and that the specified property is accessible (readable or writable) in the coded context.
* Casting an object reference assignment
* Casting an object reference parameter
* Casting an object reference to invoke a method