Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Creating Objects and Values →  CREATE OBJECT 

CREATE OBJECT - parameter_tables

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


 ... [PARAMETER-TABLE ptab] 
    [EXCEPTION-TABLE etab].

Effect

The additions PARAMETER-TABLE and EXCEPTION-TABLE pass actual parameters dynamically to the instance constructor or assign return values to the non-class-based exceptions.

These additions can be used only if the instantiated class is specified dynamically in name. Using the special internal tables ptab and etab, they assign actual parameters to the input parameters of the instance constructor or return values to the non-class-based exceptions.

The syntax and semantics are the same as those that apply to dynamic method calls using the statement CALL METHOD. The internal tables ptab and etab in particular must be defined with reference to the tables ABAP_PARMBIND_TAB and ABAP_EXCPBIND_TAB from the type group ABAP.


Example

Creates a Control Framework (CFW) dialog box dynamically and passes input parameters dynamically to the instance constructor of the global class CL_GUI_DIALOGBOX_CONTAINER. The class is defined explicitly using the addition TYPE.

DATA: container TYPE REF TO cl_gui_container, 
      exc_ref TYPE REF TO cx_root. 

DATA: class TYPE string VALUE `CL_GUI_DIALOGBOX_CONTAINER`, 
      ptab TYPE abap_parmbind_tab. 

ptab = VALUE #( ( name  = 'PARENT' 
                 kind  = cl_abap_objectdescr=>exporting 
                  value = REF #( cl_gui_container=>desktop ) ) 
                ( name  = 'WIDTH' 
                  kind  = cl_abap_objectdescr=>exporting 
                  value = REF #( 1000 ) ) 
                ( name  = 'HEIGHT' 
                  kind  = cl_abap_objectdescr=>exporting 
                  value = REF #( 300 ) ) ). 

TRY. 
    CREATE OBJECT container TYPE (class) 
      PARAMETER-TABLE ptab. 
  CATCH cx_sy_create_object_error INTO exc_ref. 
    MESSAGE exc_ref->get_text( ) TYPE 'I'. 
ENDTRY. 

Example

The following program extract creates an object of a class SOME_CLASS dynamically whose instance constructor expects a reference variable of the static type ANY_CLASS. The syntax of the program is correct, but the object is created only in systems in which the classes exist.

CONSTANTS: 
  anycls  TYPE string VALUE `ANY_CLASS`, 
  somecls TYPE string VALUE `SOME_CLASS`, 
  param   TYPE string VALUE `PARA`. 

DATA dref TYPE REF TO data. 
TRY. 
    CREATE DATA dref TYPE REF TO (anycls). 
    ASSIGN dref->* TO FIELD-SYMBOL(<fs>). 
    CREATE OBJECT <fs> TYPE (anycls). 
  CATCH cx_sy_create_data_error 
        cx_sy_create_object_error. 
    ... 
ENDTRY. 

IF <fs> IS ASSIGNED. 
  DATA(ptab) = VALUE abap_parmbind_tab( 
    ( name = param 
      kind  = cl_abap_objectdescr=>exporting 
      value =  REF #( <fs> ) ) ). 
ENDIF. 

DATA oref TYPE REF TO object. 
TRY. 
    CREATE OBJECT oref TYPE (somecls) PARAMETER-TABLE ptab. 
  CATCH cx_sy_create_object_error 
        cx_sy_dyn_call_parameter_error INTO DATA(exc). 
    ... 
ENDTRY.

This program can be executed in systems in which classes are declared as follows without raising exceptions.

CLASS any_class DEFINITION. 
  ... 
ENDCLASS. 

CLASS some_class DEFINITION. 
  PUBLIC SECTION. 
    METHODS constructor IMPORTING para TYPE REF TO any_class. 
    ... 
ENDCLASS. 

CLASS some_class IMPLEMENTATION. 
  METHOD constructor. 
    ... 
  ENDMETHOD. 
  ... 
ENDCLASS.