Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Storing and retrieving objects from an internal table

Former Member
0 Likes
2,029

Dear ABAPers,

I'm not sure how to do that in an elegant way. I need to store objects of any class (lets say that I will cast them to OBJECT) into an internal table. How could I define such a table? What would be the instructions to store and get back the objects then?

Sincerely,

Olivier MATT

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,234

Hello Olivier

Here is an example using an <b>iterator class</b> (similar like in Java):

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_ITERATOR
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_iterator.


DATA:
  gd_text        TYPE string,
  gs_viob03      TYPE viob03,
  gt_viob03      TYPE re_t_viob03,
*
  go_object      TYPE REF TO object,
  gto_list       TYPE swf_utl_object_tab,
  go_building    TYPE REF TO if_rebd_building,
  go_iterator    TYPE REF TO cl_swf_utl_iterator.


START-OF-SELECTION.

  SELECT * FROM  viob03 INTO TABLE gt_viob03
         WHERE  bukrs  = '1000'
         AND    swenr  = '00001000'.
  " selects three buildings on IDES ECC 5.0




  LOOP AT gt_viob03 INTO gs_viob03.
    CLEAR: go_building.

    CALL METHOD cf_rebd_building=>find
      EXPORTING
        id_bukrs       = gs_viob03-bukrs
        id_swenr       = gs_viob03-swenr
        id_sgenr       = gs_viob03-sgenr
         id_activity    = reca1_activity-display
         if_auth_check  = abap_false
*        IF_ENQUEUE     = ABAP_TRUE
*        IF_USE_ARCHIVE = ABAP_TRUE
      RECEIVING
        ro_instance    = go_building
       EXCEPTIONS
         error          = 1
         OTHERS         = 2.
    IF sy-subrc <> 0.
*     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    IF ( go_building IS BOUND ).
      APPEND go_building TO gto_list.
    ENDIF.

  ENDLOOP.


  " create iterator
  CREATE OBJECT go_iterator
    EXPORTING
      im_object_list = gto_list
*      IM_STARTPOINT = MC_START_TOP
      .


  CLEAR: go_building.
  go_building ?= go_iterator->get_first( ).  " casting OR
  "  go_object    = go_iterator->get_first( ).
  "  go_building ?= go_object.

  WHILE ( go_building IS BOUND ).
*   do something with the instance
    gd_text = go_building->get_text( ).
    WRITE: / go_building->ms_detail-bukrs,
             go_building->ms_detail-swenr,
             go_building->ms_detail-sgenr,
             gd_text.

    go_building ?= go_iterator->get_next( ).
  ENDWHILE.


END-OF-SELECTION.

Regards

Uwe

Dear ABAPers,

I'm not sure how to do that in an elegant way. I need to store objects of any class (lets say that I will cast them to OBJECT) into an internal table. How could I define such a table? What would be the instructions to store and get back the objects then?

Sincerely,

Olivier MATT

6 REPLIES 6
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,235

Hello Olivier

Here is an example using an <b>iterator class</b> (similar like in Java):

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_ITERATOR
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_iterator.


DATA:
  gd_text        TYPE string,
  gs_viob03      TYPE viob03,
  gt_viob03      TYPE re_t_viob03,
*
  go_object      TYPE REF TO object,
  gto_list       TYPE swf_utl_object_tab,
  go_building    TYPE REF TO if_rebd_building,
  go_iterator    TYPE REF TO cl_swf_utl_iterator.


START-OF-SELECTION.

  SELECT * FROM  viob03 INTO TABLE gt_viob03
         WHERE  bukrs  = '1000'
         AND    swenr  = '00001000'.
  " selects three buildings on IDES ECC 5.0




  LOOP AT gt_viob03 INTO gs_viob03.
    CLEAR: go_building.

    CALL METHOD cf_rebd_building=>find
      EXPORTING
        id_bukrs       = gs_viob03-bukrs
        id_swenr       = gs_viob03-swenr
        id_sgenr       = gs_viob03-sgenr
         id_activity    = reca1_activity-display
         if_auth_check  = abap_false
*        IF_ENQUEUE     = ABAP_TRUE
*        IF_USE_ARCHIVE = ABAP_TRUE
      RECEIVING
        ro_instance    = go_building
       EXCEPTIONS
         error          = 1
         OTHERS         = 2.
    IF sy-subrc <> 0.
*     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    IF ( go_building IS BOUND ).
      APPEND go_building TO gto_list.
    ENDIF.

  ENDLOOP.


  " create iterator
  CREATE OBJECT go_iterator
    EXPORTING
      im_object_list = gto_list
*      IM_STARTPOINT = MC_START_TOP
      .


  CLEAR: go_building.
  go_building ?= go_iterator->get_first( ).  " casting OR
  "  go_object    = go_iterator->get_first( ).
  "  go_building ?= go_object.

  WHILE ( go_building IS BOUND ).
*   do something with the instance
    gd_text = go_building->get_text( ).
    WRITE: / go_building->ms_detail-bukrs,
             go_building->ms_detail-swenr,
             go_building->ms_detail-sgenr,
             gd_text.

    go_building ?= go_iterator->get_next( ).
  ENDWHILE.


END-OF-SELECTION.

Regards

Uwe

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,234

This example shows how to build an internal table full of instances of classes.



report zrich_0001 .

*---------------------------------------------------------------------*
*       CLASS lcl_app DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app definition.


  public section.

    types: begin of ttab,
           fld1(10) type c,
           fld2(10)  type c,
           fld3(10)  type c,
           end of ttab.
    data: itab type table of ttab.
    data: xtab type ttab.

    methods: constructor, do_this.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_app IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app implementation.

  method constructor.

    xtab-fld1 = 'ABC'.
    xtab-fld2 = 'DEF'.
    xtab-fld3 = 'GHI'.
    append xtab to itab.

  endmethod.

  method do_this.

    loop at itab into xtab.
      write:/ xtab-fld1, xtab-fld2, xtab-fld3.
    endloop.

  endmethod.

endclass.

data: a_app type ref to lcl_app.
data: a_app_list type table of ref to lcl_app.

start-of-selection.

  do 10 times.
    create object a_app.
    append a_app to a_app_list.
  enddo.

  loop at a_app_list into a_app.
    call method a_app->do_this( ).
  endloop.

Regards,

Rich Heilman

Read only

0 Likes
1,234

Thanks for your help.

Unfortunately, I forgot to mention that I have a special case. I need to use an internal table (TYPE TABLE OF REF TO some_object_class) as an attribute of a class.

The Class Builder doesnt seem to allow me to enter such a type.

Is there any special trick to enforce this barrier? As I am pretty new to ABAP, I also wonder if there is a pure text-editing mode for classes (like in Java when you open the .java file of a class).

Any ideas?

Sincerely,

Olivier MATT

Read only

0 Likes
1,234

You need to create a type type via SE11 which is TYPE REF to your class. Then use this table type in class builder when defining your attribute.

Also, about pure text-editing, you can when it comes to local classes, but not really with global classes(SE24) <i>yet</i>. There are options in the SE24 where you can go to the public sections, the private, and so on.

Regards,

Rich Heilman

Read only

0 Likes
1,234

Hello Oliver

The table type <b>SWF_UTL_OBJECT_TAB</b> does already fulfill your requirement in its <b>most generic</b> form (reference type is class OBJECT, the ROOT class of all ABAP classes). However, in order to develop more "Java-like" you could use the iterator class <b>CL_SWF_UTL_ITERATOR</b> as<i> instance attribute</i> of your class.

Regards

Uwe

Read only

Former Member
0 Likes
1,234

Please consider that there isn't in ABAP a concept of 'generics', like the last Java versions.

So the standard iterator class CL_SWF_UTL_ITERATOR operates on an internal table based on the generic OBJECT superclass. the programmer should take care to handle properly the actual types.

Regards,

Massimo