NEW function (classes)

Creates an instance of a class (object) and returns an object reference to that instance. You can use this object reference to access the PUBLIC data members, properties, and methods of the instance. For more information on object references, see the reference entry for a Class-based object reference.

Syntax

NEW object-type-name ( [ parameter[ , parameter ]...] )
object-type-name
Specifies the type name of the ABL or .NET class you want to instantiate. Specify a class type name using the syntax as described in the Type-name syntax reference entry. If object-type-name specifies a .NET object, it can be most any .NET class, with a few restrictions. For more information, see the Notes of this reference entry. The value of object-type-name is restricted to alphanumeric characters plus the symbols #, $, %, and _.

If, without casting, you assign the NEW function value to a target data element, or pass the NEW function as input to a target routine parameter, object-type-name must specify one of the following classes:

  • The same class type that is defined for the target
  • A subclass of the class type that is defined for the target
  • A class type that implements the interface type defined for the target

If object-type-name specifies one of the following kinds of object types, ABL raises a compiler error:

  • An interface type
  • An abstract class type
( [ 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. To invoke a constructor that is overloaded in the class, you must specify sufficient information for each parameter to disambiguate it from all other constructors in the class. Otherwise, ABL raises an error identifying the ambiguity.

For information on the parameter passing syntax and disambiguating overloaded constructors, see the Parameter passing syntax reference entry.

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

Examples

The following example shows how the NEW function can instantiate a class within an expression:

DEFINE VARIABLE rObj AS Progress.Lang.Object.

IF ( NEW Progress.Lang.Object( ) ):ToString( ) BEGINS "Progress" THEN
  MESSAGE "This object is making Progress..." VIEW-AS ALERT-BOX.

This code fragment instantiates a Progress.Lang.Object in order to check if the string value returned from its ToString( ) method begins with "Progress", and displays a message if it does.

The following example shows three (3) invocations of the NEW function.

r-newclass.p

ROUTINE-LEVEL ON ERROR UNDO, THROW.

FUNCTION ClassFunc RETURNS CLASS Progress.Lang.Object 
  (INPUT iVal AS INTEGER):
  MESSAGE "iVal = " iVal VIEW-AS ALERT-BOX.
  IF iVal MODULO 2 = 0 THEN
    RETURN NEW Progress.Lang.Object( ).
  ELSE
    RETURN?.
END FUNCTION.

RUN DisplayClass( NEW Progress.Lang.Object( ) ).

PROCEDURE DisplayClass:
  DEFINE INPUT PARAMETER rObj AS CLASS Progress.Lang.Object.

  DEFINE VARIABLE cClass1 AS CHARACTER NO-UNDO.
  DEFINE VARIABLE cClass2 AS CHARACTER NO-UNDO.

  cClass1 = rObj:ToString( ).
  /* The ToString( ) call errors on an odd RANDOM input value */
  cClass2 = ( ClassFunc( RANDOM(1,100) ) ):ToString( ).

  MESSAGE "The " cClass1 " and " cClass2 " instances have the same 
  class type." 
    VIEW-AS ALERT-BOX.

  CATCH e AS Progress.Lang.SysError:
    UNDO, THROW NEW Progress.Lang.AppError( "Application Error", 555 ).
  END CATCH.
END PROCEDURE.

This r-newclass.p procedure runs an internal procedure (DisplayClass) that displays a message showing the ToString( ) values of two different objects instantiated as the same class type (Progress.Lang.Object). However, one of these objects is instantiated within a user-defined function (ClassFunc) that returns a valid object reference or the Unknown value (?) in order to generate an error, depending on the input value of a RANDOM function.

When a valid object reference is returned, it is used to access the ToString( ) method of the instance in an expression. When the Unknown value (?) is returned, this generates a run-time error when used as an object reference. The procedure then catches the error and responds by throwing a Progress.Lang.AppError object that is also instantiated by a NEW function invoked in an expression, and the error text is displayed as an error message when run in the OpenEdge Editor.

Notes

See also

Assignment (=) statement, Class-based object reference, CLASS statement, CONSTRUCTOR statement, DELETE OBJECT statement, DYNAMIC-NEW statement, FIRST-OBJECT attribute, FUNCTION statement, LAST-OBJECT attribute, METHOD statement, New( ) method, NEW statement, Parameter passing syntax, Type-name syntax, USING statement