2007 Mar 02 2:02 PM
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
2007 Mar 02 2:34 PM
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
2007 Mar 02 2:34 PM
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
2007 Mar 02 2:58 PM
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
2007 Mar 02 3:18 PM
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
2007 Mar 02 3:26 PM
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
2007 Mar 02 8:07 PM
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
2008 Jan 02 2:37 PM
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
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |