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

Class reference internal table

former_member205645
Participant
0 Likes
1,409

Hello Gurus,

i have an internal table filled with class references and i need to acces the one field of a structure of every class.

I am now trying something like this:


loop ref_table assigning <l_wrk_ref_but0id>.

      IF <l_wrk_ref_but0id>->m_str_but0id-type = /gkv/cd40_cl_const=>con_pkkv.

      ENDIF.

endloop.

I need the field "type" in the structure "m_str_but0id" of every class (reference) in the table. The error that i'm getting is: "Field m_str_but0id unknown".

Please help.

Ioan Constantin.

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
924

Hello Ioan

You have to access the field dynamically as well:


DATA:
   ld_structure  TYPE <name of structure>,
   ld_attribute    TYPE tabname,
   ld_field          TYPE fieldname.

FIELD-SYMBOLS:
  <ls_struct>     TYPE any,
  <ld_fld>           TYPE any.

ld_attribute = 'M_STR_BUT0ID'.
ld_field       = 'TYPE'.  
loop ref_table assigning <l_wrk_ref_but0id>.

     ASSIGN <l_wrk_ref>but0id>->(ld_attribute) TO <ls_struct>.
     ASSIGN COMPONENT (ld_field) OF STRUCTURE <ls_struct> TO <ld_fld>.

"      IF <l_wrk_ref_but0id>->m_str_but0id-type = /gkv/cd40_cl_const=>con_pkkv.
       IF ( <ld_fld> = /gkv/cd40_cl_const=>con_pkkv ).
          ... 
      ENDIF.
 
endloop.

Even simpler might be the following approach:


LOOP AT ref_table assigning <l_wrk_ref_but0id>
                WHERE ( table_line->m_str_but0id-type = /gkv/cd40_cl_const=>con_pkkv ).
...
ENDLOOP.
" Assumption: Itab has class reference type as line type.

Regards

Uwe

4 REPLIES 4
Read only

uwe_schieferstein
Active Contributor
0 Likes
925

Hello Ioan

You have to access the field dynamically as well:


DATA:
   ld_structure  TYPE <name of structure>,
   ld_attribute    TYPE tabname,
   ld_field          TYPE fieldname.

FIELD-SYMBOLS:
  <ls_struct>     TYPE any,
  <ld_fld>           TYPE any.

ld_attribute = 'M_STR_BUT0ID'.
ld_field       = 'TYPE'.  
loop ref_table assigning <l_wrk_ref_but0id>.

     ASSIGN <l_wrk_ref>but0id>->(ld_attribute) TO <ls_struct>.
     ASSIGN COMPONENT (ld_field) OF STRUCTURE <ls_struct> TO <ld_fld>.

"      IF <l_wrk_ref_but0id>->m_str_but0id-type = /gkv/cd40_cl_const=>con_pkkv.
       IF ( <ld_fld> = /gkv/cd40_cl_const=>con_pkkv ).
          ... 
      ENDIF.
 
endloop.

Even simpler might be the following approach:


LOOP AT ref_table assigning <l_wrk_ref_but0id>
                WHERE ( table_line->m_str_but0id-type = /gkv/cd40_cl_const=>con_pkkv ).
...
ENDLOOP.
" Assumption: Itab has class reference type as line type.

Regards

Uwe

Read only

0 Likes
924

Hello Uwe,

thank you for the response. What do you mean with "table_line" ? Yes the table line is of type class reference.


DATA: ref_table type tab_but0id_ref. " tab_but0id_ref ... table type with line type class reference ( ref to cl_99_dat_obj_but0id)

FIELD-SYMBOLS: <l_wrk_ref_but0id> type ref ro cl_99_dat_obj_but0id.


LOOP AT ref_table assigning <l_wrk_ref_but0id>
                WHERE ( table_line->m_str_but0id-type = /gkv/cd40_cl_const=>con_pkkv ).
...
ENDLOOP.

Regards,

Ioan.

Read only

0 Likes
924

What Uwe 'means' is this (actually it is not really 'means'): table_line is used in generic programming. It is not really the name of an internal table line, but more a generic name for 'all' internal tables. So as you can see in the example from Uwe is the generic name 'table_line'.

Read only

0 Likes
924

As far as generic access (not specified type of object being access until runtime) is concerned, Guys are of course right, but as you know line type of table, which is fully typed (specifeid as ref to cl_99_dat_obj_but0id ) there can be a problem of attribute's visibility within class. Ensure that is it a public attribute, not protected or private, otherwise the acces from the outside will not be possible, hence you get the error.

Regards

Marcin