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

Dynamic table as class (instance) attribute

Former Member
0 Likes
4,605

Hey folks,

I am currently developing a component class that should be able to accept elements of an arbitrary type (all the same) and store them in a dynamic table (instance attribute).

Let's say I want the following

DATA my_list TYPE REF TO ZCL_MY_LIST.
CREATE OBJECT my_list.
my_list->add(
     EXPORTING
          element = 'hello'
).
my_list->add(
     EXPORTING
          element = 'blub'
).

The class ZCL_MY_LIST has an attribute list of TYPE REF TO DATA and is initialized by the first call to add based on the type of the parameter. Unfortunately I am not able to add further elements to the table, since I cannot call APPEND on me->list (because it is of TYPE REF TO DATA).

Since I cannot use field symbols in the class instance context, does anyone have an idea on how to store this dynamic table as an attribute of the class while keeping it editable?

Thanks in advance,

David

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,227

Hi David,

Instead of using the APPEND statement inside the method.

Try the below code

Field-Symbols: <FS_TAB> TYPE standard table,

                         <FS_T_LIST> TYPE standard table.

CREATE DATA list LIKE <FS_TAB> --> Use this statement the first time only

ASSIGN list->* TO <FS_T_LIST>.

do the regular append based on the types to the table <FS_T_LIST>.

Regards,

Vasanth

5 REPLIES 5
Read only

Former Member
0 Likes
2,228

Hi David,

Instead of using the APPEND statement inside the method.

Try the below code

Field-Symbols: <FS_TAB> TYPE standard table,

                         <FS_T_LIST> TYPE standard table.

CREATE DATA list LIKE <FS_TAB> --> Use this statement the first time only

ASSIGN list->* TO <FS_T_LIST>.

do the regular append based on the types to the table <FS_T_LIST>.

Regards,

Vasanth

Read only

0 Likes
2,227

Thanks that worked out nicely!

Read only

Former Member
0 Likes
2,227

You can use field symbols - you just can't define them as global (which in my opinion is a good thing....)

Read only

tolga_polat
Active Participant
0 Likes
2,227

Hi David,

When using type ref to data, you have to assign this object to field-symbol.

Definition part.


PUBLIC SECTION.

     METHODS : constructor,

               add

                IMPORTING

                  element TYPE any.

   PRIVATE SECTION.

     DATA : list TYPE REF TO data.

implementation part.


METHOD constructor.

     CREATE DATA me->list TYPE STANDARD TABLE OF string. " Created only one time

   ENDMETHOD.                    "constructor

   METHOD add.

     FIELD-SYMBOLS : <list> TYPE STANDARD TABLE.

     ASSIGN me->list->* TO <list>.

     CHECK sy-subrc EQ 0.

     APPEND element

         TO <list>.

   ENDMETHOD.                    "add

You can use something like this, but you cant use generic type for me->list.

If you dont use type standart table of syntax, you cant create table type.

READ TABLE or LOOP AT ... WHERE is problematic with this kind of table.

Best Regards.

Tolga

Read only

former_member184158
Active Contributor
0 Likes
2,226

Hi

I would like to add some info with image if it is allowed, maybe for others who look for an answer, which could help them.,

REPORT  zibo_test1.

CLASS lcl_test DEFINITION.

 
PUBLIC SECTION.
   
DATA mr_data TYPE REF TO data .
   
METHODS add
   
IMPORTING
       iv_val
TYPE any .
   
METHODS constructor .
ENDCLASS.                 

CLASS lcl_test IMPLEMENTATION.
 
METHOD add.
   
FIELD-SYMBOLS: <lt_data> TYPE STANDARD TABLE .
   
ASSIGN mr_data->* TO <lt_data>.
   
APPEND iv_val TO <lt_data>.
 
ENDMETHOD.                    "add


 
METHOD constructor.
   
CREATE DATA mr_data TYPE STANDARD TABLE OF string.
 
ENDMETHOD.                    "constructor

ENDCLASS.                    "lcl_test IMPLEMENTATION


DATA lr_test TYPE REF TO lcl_test.

START-OF-SELECTION.
 
CREATE OBJECT lr_test.

  lr_test
->add( iv_val = '1' ).
  lr_test
->add( iv_val = '2' ).
  lr_test
->add( iv_val = '2' ).

Regards

Ibrahim