DYNAMIC-NEW statement

Creates an instance of a class (object) whose class type is specified by a run-time expression, and assigns its object reference to an appropriately defined ABL data element. Once assigned, you can use the object reference to access this class instance and its PUBLIC data members, properties, and methods. For more information on object references, see the reference entry for a Class-based object reference.

Syntax

object-reference = DYNAMIC-NEW expression 
  ( [ parameter [ , parameter ]... ] ) [ NO-ERROR ]
object-reference
The name of an ABL data element to which you want to assign the object reference of a new instance of the class specified by expression. This data element must be defined as a compatible class or interface type, and can be one of the following:

To be compatible, the data type of object-reference must be:

  • The same class as the class specified by expression
  • A super class of the class specified by expression
  • An interface that is implemented by the class specified by expression
expression
A character expression that evaluates to a fully qualified class type name for the ABL or .NET class you want to instantiate. This expression must specify a class type name as described in the Type-name syntax reference entry, except that you must always specify the complete type name; any present USING statement has no effect. If no package (or namespace for a .NET class) is specified, the class name must represent the complete type name.

This expression cannot evaluate to:

  • An ABL built-in class type name, such as Progress.Lang.Object
  • The type name of an interface or abstract class
( [ parameter [ , parameter ]... ] )
Specifies zero or more parameters passed to a PUBLIC instance constructor that is defined for the class. You must provide the parameters identified by the specified constructor, matched with respect to number, data type, and mode.

For information on the parameter passing syntax, see the Parameter passing syntax reference entry.

For information on defining a constructor for a class, see the CONSTRUCTOR statement reference entry.

NO-ERROR
Suppresses ABL errors or error messages that would otherwise occur and diverts them to the ERROR-STATUS system handle. If an error occurs, the action of the statement is not done and execution continues with the next statement. If the statement fails, any persistent side-effects of the statement are backed out. If the statement includes an expression that contains other executable elements, like methods, the work performed by these elements may or may not be done, depending on the order the AVM resolves the expression elements and the occurrence of the error.

For the DYNAMIC-NEW statement with NO-ERROR, if ERROR is raised, then the object-reference remains unchanged. If a RETURN statement or an UNDO statement with the THROW or RETURN ERROR options in a constructor raises ERROR and also returns an error string, you can obtain this string value after the assignment statement completes using the RETURN-VALUE function.

To check for errors after a statement that uses the NO-ERROR option:

  • Check the ERROR-STATUS:ERROR attribute to see if the AVM raised the ERROR condition.
  • Check if the ERROR-STATUS:NUM-MESSAGES attribute is greater than zero to see if the AVM generated error messages. ABL handle methods used in a block without a CATCH end block treat errors as warnings and do not raise ERROR, do not set the ERROR-STATUS:ERROR attribute, but do add messages to the ERROR-STATUS system handle. Therefore, this test is the better test for code using handle methods without CATCH end blocks. ABL handle methods used in a block with a CATCH end block raise ERROR and add messages to the error object generated by the AVM. In this case, the AVM does not update the ERROR-STATUS system handle.
  • Use ERROR-STATUS:GET-MESSAGE( message-num ) to retrieve a particular message, where message-num is 1 for the first message.

If the statement does not include the NO-ERROR option, you can use a CATCH end block to handle errors raised by the statement.

Some other important usage notes on the NO-ERROR option:

  • NO-ERROR does not suppress errors that raise the STOP or QUIT condition.
  • A CATCH statement, which introduces a CATCH end block, is analogous to a NO-ERROR option in that it also suppresses errors, but it does so for an entire block of code. It is different in that the error messages are contained in a class-based error object (generated by the AVM or explicitly thrown), as opposed to the ERROR-STATUS system handle. Also, if errors raised in the block are not handled by a compatible CATCH block, ON ERROR phrase, or UNDO statement, then the error is not suppressed, but handled with the default error processing for that block type.
  • When a statement contains the NO-ERROR option and resides in a block with a CATCH end block, the NO-ERROR option takes precedence over the CATCH block. That is, an error raised on the statement with the NO-ERROR option will not be handled by a compatible CATCH end block. The error is redirected to the ERROR-STATUS system handle as normal.
  • If an error object is thrown to a statement that includes the NO-ERROR option, then the information and messages in the error object will be used to set the ERROR-STATUS system handle. This interoperability feature is important for those integrating code that uses the traditional NO-ERROR technique with the newer, structured error handling that features error objects and CATCH end blocks.

Example

The following contrived (non-compiling) procedure fragment shows the instantiation of a new class type specified with a variable:

/* Can be set to a subclass type name */
DEFINE INPUT PARAMETER myBusObjParm AS CHARACTER NO-UNDO.

/* Procedure only knows about the base class */
DEFINE VARIABLE myBusObj AS CLASS acme.myObjs.BusObj NO-UNDO.

myBusObj = DYNAMIC-NEW myBusObjParm ( ). /* Create the passed subclass */
 myBusObj:getData( ).                    /* Invoke base class method
                                             polymorphically on subclass */

In this case, the procedure assumes that it is operating on a class, acme.myObjs.BusObj, that is the base class for several subclasses, each of which implements the same set of operations for its own purposes, such as the getData( ) method shown. When the procedure is called, its INPUT parameter is passed a character string that evaluates to a subclass type name, such as "acme.myObjs.CustObj", which it then uses to instantiate a class of that type using the DYNAMIC-NEW statement. Thus, the same procedure can be called to instantiate and operate on different subclasses of the same base class, as determined by run-time conditions.

Notes

See also

Assignment (=) statement, Class-based object reference, CLASS statement, DYNAMIC-CAST function, DYNAMIC-INVOKE function, Invoke( ) method (Class), NEW function (classes), New( ) method, NEW statement, Parameter passing syntax, Progress.Lang.ParameterList class