New( ) method
- Last Updated: January 16, 2024
- 2 minute read
- OpenEdge
- Version 12.8
- Documentation
Syntax
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 Use the 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:
|
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.
|
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:
|
Element descriptions for this syntax diagram follow:
- parameterlist-object
- An instance of the
Progress.Lang.ParameterListclass.
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 ABL Reference and Use 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:
|
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.