PreviousNextIndex

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
( [ parameter [ , parameter ] ... ] )
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


OpenEdge Release 10.2B
Copyright © 2009 Progress Software Corporation
PreviousNextIndex