Try OpenEdge Now
skip to main content
Object-oriented Programming
Programming with Class-based Objects : Instantiating and managing class-based objects : Creating a class instance : New( ) method
 
New( ) method
You can use the New( ) method of the Progress.Lang.Class class to dynamically create an instance of a class when the class name and any parameters passed to it are only known at run time. This method returns a Progress.Lang.Object, which provides an instance of a class.
The New( ) method is one of several reflection methods of the Progress.Lang.Class class that provide type information about a class or interface at run time. For more information about the reflection capabilities of the Progress.Lang.Class class, see Usingthe Progress.Lang.Class class.
Two overloaded versions of the New( ) method are supported. You can use the first version when the constructor of the class does not take any arguments. This is the syntax for the New( ) method when no parameters are passed to its constructor:

Syntax

New( )
In this example of the first overloaded version, the type name of the class to be instantiated is passed in as a CHARACTER string, pcDynTypeName. The GetClass( ) method of the Progress.Lang.Class class is used to get the fully-qualified object type name of the pcDynTypeName-specified class. The constructor for the pcDynTypeName-specified class does not take any parameters, so the New( ) method is called without parameters.
DEFINE INPUT PARAMETER pcDynTypeName AS CHARACTER NO-UNDO.
DEFINE VARIABLE rObjClass  AS CLASS Progress.Lang.Class  NO-UNDO.
DEFINE VARIABLE rDynBusObj AS CLASS Progress.Lang.Object NO-UNDO.

rObjClass  = Progress.Lang.Class:GetClass(pcDynTypeName).
rDynBusObj = rObjClass:New( ).
Note: Because the New( ) method returns an instance of the Progress.Lang.Object class, the result of the New( ) method must be assigned to a Progress.Lang.Object object. You can then cast the object to the appropriate subclass, as shown in the next example using the CAST function. The New( ) method must be coded as a standalone statement and not part of an ASSIGN statement.
You can use the second overloaded version when the constructor of the class takes zero or more parameters. This is the syntax for the New( ) method when the constructor of its class takes zero or more parameters:
New( parameterlist-object )
Element descriptions for this syntax diagram follow:
parameterlist-object
An instance of the Progress.Lang.ParameterList class.
Parameters are passed to the New( ) method as a Progress.Lang.ParameterList object. Since a Progress.Lang.ParameterList object can be built with zero or more parameters, this version of the New( ) method is used to instantiate any class, even those classes whose constructor does not take any arguments. For more information about the Progress.Lang.ParameterList object, see OpenEdge Development: ABL Reference and Using the Progress.Lang.ParameterList class.
In this example of the second overloaded version of the New( ) method, the rParamList parameter list object is passed to the pcDynTypeName-specified class constructor when the New( ) method is invoked:
DEFINE INPUT PARAMETER pcDynTypeName AS CHARACTER NO-UNDO.

DEFINE VARIABLE iValue AS CLASS INTEGER INITIAL 10 NO-UNDO.
DEFINE VARIABLE rDynBusObj AS CLASS BusinessObject NO-UNDO.
DEFINE VARIABLE rObjClass AS CLASS Progress.Lang.Class NO-UNDO.
DEFINE VARIABLE rParamList AS CLASS Progress.Lang.ParameterList NO-UNDO.

ASSIGN
  rParamList =NEW Progress.Lang.ParameterList(1)
  rObjClass  = Progress.Lang.Class:GetClass(pcDynTypeName).

rParamList:SetParameter(1, "INTEGER", "INPUT", iValue).
rDynBusObj = CAST(rObjClass:New(rParamList), BusinessObject).
The class instance referenced by rDynBusObj is not a Progress.Lang.Object object, but rather a BusinessObject. Thus, a cast is necessary for the compiler to allow the assignment of the New( ) method result into the subclass referenced by rDynBusObj. For more information on casting object references, see Object reference assignment and casting.