Skip to Content

SAP syntax for Class creation:

CREATE OBJECT

CREATE OBJECT oref
              TYPE {class|(name)} [parameter list].

… PARAMETER-TABLE ptab

… EXCEPTION-TABLE etab 

Effect

We can use these additions when the instanced class is specified dynamically in name.
You use the special internal tables ptab and etab to assign actual parameters to the input parameters of
the instance constructor or return values to not class-based exceptions.
The internal tables
ptab and etab must be defined with reference to the tables ABAP_PARMBIND_TAB and ABAP_EXCPBIND_TAB.

Sample program where the class name is passed as variable and the exporting parameter is passed in Parameter-table.

Report: z_dyn_classname.

CLASS cl_abap_objectdescr DEFINITION LOAD.DATA: container TYPE ref to object,
      pic_container
TYPE ref to CL_GUI_CONTAINER,
      exc_ref
TYPE REF TO cx_root,
      lo_cast_error
TYPE REF TO cx_sy_move_cast_error,
      exc_text
TYPE string.* Picture ControlDATA picture TYPE REF TO cl_gui_picture.
DATA: class TYPE string VALUE ‘CL_GUI_DIALOGBOX_CONTAINER’,
      ptab
TYPE abap_parmbind_tab,
      ptab_line
TYPE abap_parmbind.
Call screen 100.

Module STATUS_0100 output.

ptab_line-name = ‘WIDTH’.
ptab_line-kind = cl_abap_objectdescr=>exporting.GET REFERENCE OF 1000 INTO ptab_line-value.INSERT ptab_line INTO TABLE ptab.

ptab_line-name = ‘PARENT’.
ptab_line-kind = cl_abap_objectdescr=>exporting.GET REFERENCE OF CL_GUI_CONTAINER=>DESKTOP
             
INTO ptab_line-value.INSERT ptab_line INTO TABLE ptab.

ptab_line-name = ‘HEIGHT’.
ptab_line-kind = cl_abap_objectdescr=>exporting.GET REFERENCE OF 300 INTO ptab_line-value.INSERT ptab_line INTO TABLE ptab.
TRY.
   
CREATE OBJECT container TYPE (class)
                 
PARAMETERTABLE ptab.

/wp-content/uploads/2013/04/first_204756.jpg
 
CATCH cx_sy_create_object_error INTO exc_ref.
    exc_text = exc_ref->get_text( ).
   
MESSAGE exc_text TYPE ‘I’.

ENDTRY.TRY.

* Now, the Widening cast to move the reference from the* generic object to dialogbox parent container class (more specific class).
pic_container ?= container.CATCH cx_sy_move_cast_error INTO lo_cast_error.
     
WRITE: / ‘Widening cast failed’.
 
ENDTRY.
CREATE OBJECT picture
       
EXPORTING parent = pic_container.
* Request an URL from the data provider by exporting the pic_data.
   
DATA url(255).
   
CLEAR URL.
   
PERFORM load_pic_from_db CHANGING url.
* load picture
   
CALL METHOD picture->load_picture_from_url
       
EXPORTING url = url.

    CALL METHOD cl_gui_cfw=>flush
        
EXCEPTIONS cntl_system_error = 1
                    cntl_error =
2.
   
IF sy-subrc <> 0.* error handling
   
ENDIF.endmodule.                 ”STATUS_0100 OUTPUT

FORM load_pic_from_db CHANGING url.
 
DATA query_table LIKE w3query OCCURS 1 WITH HEADER LINE.
 
DATA html_table LIKE w3html OCCURS 1.
 
DATA return_code LIKE w3param-ret_code.
 
DATA content_type LIKE  w3param-cont_type.
 
DATA content_length LIKE  w3param-cont_len.
 
DATA pic_data LIKE w3mime OCCURS 0.
 
DATA pic_size TYPE i.

  REFRESH query_table.
  query_table-name =
‘_OBJECT_ID’.
  query_table-
value = ‘ENJOYSAP_LOGO’.
 
APPEND query_table.

  CALL FUNCTION ‘WWW_GET_MIME_OBJECT’
      
TABLES
            query_string        = query_table
            html                = html_table
            mime                = pic_data
      
CHANGING
            return_code         = return_code
            content_type        = content_type
            content_length      = content_length
      
EXCEPTIONS
            OBJECT_NOT_FOUND    =
1
            parameter_not_found =
2
           
OTHERS              = 3.
 
IF sy-subrc = 0.
    pic_size = content_length.
 
ENDIF.

  CALL FUNCTION ‘DP_CREATE_URL’
      
EXPORTING
           
type     = ‘image’
            subtype  = cndp_sap_tab_unknown
           
size     = pic_size
            lifetime = cndp_lifetime_transaction
      
TABLES
           
data     = pic_data
      
CHANGING
            URL      = url
      
EXCEPTIONS
           
OTHERS   = 1.
ENDFORM.                               ”LOAD_PIC_FROM_DB

Expected output:

/wp-content/uploads/2013/04/first_204756.jpg

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply