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 Instance Objects in an 'Array'?

Former Member
0 Likes
2,037

Hello,

I'm a former JAVA Developer and trying to use the Features of ABAP Objects as much as possible. Now I got the problem that I have to store several instances of the same class. The number of instances is dynamic and I have to access these instances in a loop merely at the end of the program for logging purposes. Is this possible? I'm working on release 4.6c.

Thank you in advance for your help.

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,125

Hello Steffen

If class <b>CL_SWF_UTL_ITERATOR</b> is available on 4.6c (available on 6.20 and higher) then you can use this iterator class for storing your instances. Alternatively, you could define your own data type for storing instances, e.g.:

TYPES: BEGIN OF ty_s_iterator.
TYPES:   instance  TYPE REF TO object.
TYPES: END OF ty_s_iterator.
TYPES: ty_t_iterator  TYPE STANDARD TABLE OF ty_s_iterator 
                                WITH DEFAULT KEY.

DATA: 
  ls_list   TYPE ty_s_iterator,
  lt_list   TYPE ty_t_iterator.

You can add any reference type to this list. When reading the list you need to do the appropriate casting.

Regards

Uwe

7 REPLIES 7
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,126

Hello Steffen

If class <b>CL_SWF_UTL_ITERATOR</b> is available on 4.6c (available on 6.20 and higher) then you can use this iterator class for storing your instances. Alternatively, you could define your own data type for storing instances, e.g.:

TYPES: BEGIN OF ty_s_iterator.
TYPES:   instance  TYPE REF TO object.
TYPES: END OF ty_s_iterator.
TYPES: ty_t_iterator  TYPE STANDARD TABLE OF ty_s_iterator 
                                WITH DEFAULT KEY.

DATA: 
  ls_list   TYPE ty_s_iterator,
  lt_list   TYPE ty_t_iterator.

You can add any reference type to this list. When reading the list you need to do the appropriate casting.

Regards

Uwe

Read only

0 Likes
1,125

Hello again,

thanks for your very fast responses. The class CL_SWF_UTL_ITERATOR isn't available on our system. The type definition is working, but how can I declare this as an global class attribute in Class Builder SE24? I can obviously only use 'Like', 'Type' and 'Type Ref To'?

Read only

0 Likes
1,125

Hi Steffen ,

I think you can do it by creating your own types in SE11 .

You can create a table type with reference of your class .

Regards

Read only

0 Likes
1,125

Hello Steffen

If you want to have the object list as public attribute you need a <b>DDIC </b>type. Have a look at the following sample report (created on our 4.6c system):

*&---------------------------------------------------------------------*
*& Report  ZUS_INSTANCE_LIST                                           *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  zus_instance_list             .


DATA:
  go_docking    TYPE REF TO cl_gui_docking_container,
  gs_list       TYPE zus_instance,
  gt_list       TYPE zus_instance_ttyp.
" NOTE: ZUS_INSTANCE has been defined as structure with:
"       component     componenttype
"       INSTANCE      OBJECT

" ZUS_INSTANCE_TTYP is a table type with line type ZUS_INSTANCE

START-OF-SELECTION.


" Add instances to list
  DO 10 TIMES.
    CREATE OBJECT go_docking.

    gs_list-instance = go_docking.
    APPEND gs_list TO gt_list.
  ENDDO.

" Read instances from list
  LOOP AT gt_list INTO gs_list.
    go_docking ?= gs_list-instance.
  ENDLOOP.

END-OF-SELECTION.

Regards

Uwe

Read only

0 Likes
1,125

I found a way by defining a local type in the class. With the Shortcut CTRL + F5 it is possible to define the types directly in the source code.

Read only

Former Member
1,125

Hi Steffen,

You can create an array of instances of a class by using the keyword

'TYPE TABLE OF REF TO'.

For eg: say <b>ZSP_CLASS</b> is the class, for which you have to create an array of instances.

Let <b>inst_tab</b> be the int.table storing the array of instances.

Declaration of inst_tab should be like this.

data: inst_tab <b>type table of ref to</b> ZSP_CLASS.

Hope this helps you.

Regards,

SP.

Read only

0 Likes
1,125

And the loop would then look like this.

data: inst_tab type table of ref to ZSP_CLASS,
        oref type ref to ZSP_CLASS.

loop at inst_tab into oref.
endloop.

cheers

Thomas