CAST function
- Last Updated: February 11, 2026
- 3 minute read
- OpenEdge
- Version 13.0
- Documentation
Returns a new object reference to the same class instance as an existing object reference, but with a different data type. This different data type is cast from the object type of the original object reference according to another specified object type. The two object types must be related, where one is a class type and the other is a subclass of that class type or where one is an interface type and the other is a class that implements the interface of that type.
When you cast an object reference, ABL treats it as if it referenced an instance of the object type to which it is cast. The underlying class hierarchy of the object instance does not change.
Syntax
|
- object-reference
- An object reference defined with the object type to be cast.
- object-type-name
- Specifies the type name of an ABL or .NET class or interface type to which the object reference is cast. This object type must be a class type in a class hierarchy that includes or implements the object-reference data type or it must be an interface type that the object-reference data type implements. Specify the object type name using the syntax described in the Type-name syntax reference entry. With an appropriate USING statement, you can also specify an unqualified class or interface name alone.
Notes
- You typically cast an object reference down a class hierarchy—that is, from a super class to a derived class within a class hierarchy, or from an interface to a class that implements that interface. However, you do not always need to explicitly cast an object reference. Because a derived class contains all the super classes in its inherited class hierarchy, ABL implicitly casts any object reference up within its class hierarchy, and because a class that implements an interface implements all of the methods specified for the interface, ABL implicitly casts any object reference from an implementing class to any interface that the class implements.
- At compile time, ABL verifies that the specified object type is within the class hierarchy of the specified object reference. At run time, the AVM checks the validity of the cast operation. Therefore, if you access a class member on the cast object reference that exists for the cast data type, but the referenced object at run time does not actually define the accessed class member, the AVM raises ERROR at run time.
- A .NET generic type can be part of a cast.
For example, you can cast from a
System.Objectto aSystem.Collections.Generic.List<SHORT>, because all .NET classes, including generic classes, derive from the .NET root class. However, note that you cannot cast from aSystem.Collections.Generic.List<System.Object>to aSystem.Collections.Generic.List<System.Windows.Forms.Button>. You cannot assign aList<Button>reference to an object reference defined as aList<Object>, because, even though the type parameters are compatible, the two objects as a whole are not equivalent and have no inheritance relationship. Therefore, a cast between these two objects cannot work either. For more information on .NET generic types, see the Data types reference entry. - You can also use the DYNAMIC-CAST function to cast object references to object types determined at run time. This is especially useful in object-oriented applications that conform to the OpenEdge Reference Architecture (OERA). For more information on the OERA, see Progress Communities: https://community.progress.com/.
- You can use the
CASTfunction to cast a parameter in a parameter list for a method using the following syntax:method-name( INPUT CAST( object-reference, subclass-name ), ... ). - You can use the
CASTfunction to cast a temp-table field, which is defined as aProgress.Lang.Object, to use as an object of another class type. For example:DEFINE VARIABLE rCustObj AS CLASS acme.myObjs.CustObj. DEFINE TEMP-TABLE mytt FIELD CustObj AS Progress.Lang.Object. rCustObj = CAST(mytt.CustObj, acme.myObjs.CustObj).You can now use the object reference in
RCustObjto invoke methods in theacme.myObjs.CustObjclass. - You can use the
CASTfunction to cast an object reference to a subclass and invoke a method defined in that subclass using the following syntax:CAST( object-reference, object-type-name ):method-name( parameters ).You can also use this syntax to invoke a method on a class that implements the referenced interface from which you cast the specified class.
-
If using user-defined generics, you cannot use a type parameter by itself in the generic class implementation where a type would be specified for the
TYPE-OF()andCAST()built-in functions. For example:CLASS MyGeneric<T AS Animal>: METHOD VOID makeNoise(myPLO AS Progress.Lang.Object): IF TYPE-OF(myPLO, T) //compiler error: T is a type parameter THEN CAST(myPLO, T):Sound(). //compiler error END METHOD. END CLASS.In such cases, you can use the constraint type, instead of
T. For example:CLASS MyGeneric <T AS Animal>: METHOD VOID makeNoise(myPLO AS Progress.Lang.Object): IF TYPE-OF(myPLO, Animal) THEN CAST(myPLO, Animal):Sound(). END METHOD. END CLASS.