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

Using the DYNAMIC-CAST function

You can thus use the DYNAMIC-CAST function in some of the same ways as a CAST function:
*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
However unlike the CAST function, you cannot use the DYNAMIC-CAST function to directly access a method, property, or data member on the function return value without first assigning that value to an appropriate object-reference data element for accessing the class member.
Syntax descriptions for each of the permitted uses of the DYNAMIC-CAST function follow.
This is the syntax for the DYNAMIC-CAST function when used to assign an object reference to another object reference data element:

Syntax

target-object-reference = DYNAMIC-CAST
  ( object-reference , target-type-expression )
This is the syntax for the DYNAMIC-CAST function when used to pass an object reference to an INPUT parameter:
routine-name ( [ INPUT ] DYNAMIC-CAST
  ( object-reference , target-type-expression ) )
Element descriptions for these syntax diagrams follow:
target-object-reference
A data element defined as the object reference type specified by target-type-expression, 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 the class type specified by target-type-expression, or an interface implemented by a class type specified by target-type-expression. 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 object type specified by target-type-expression.
target-type-expression
A character expression that evaluates to 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 must be a fully qualified object type name. Because target-type-expression is evaluated at run time, any USING statements have no affect on its evaluation. For more information on object type names, see Defining and referencing object type names.
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 object type specified by target-type-expression.