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 Type creation: structure

steven_dierick
Participant
0 Likes
381

Hey,

I made a dynamic table and now I want to loop this table.

But I can't seem to make a structure to use in the loop.

I get the error 'data object has no structure'.

This is what I already have.

data: linetype type ref to cl_abap_structdescr,

tabletype type ref to cl_abap_tabledescr,

lt_comp type cl_abap_structdescr=>component_table,

lr_table type ref to data,

lr_line type ref to data.

field-symbols: <lt_data> type table,

<ls_data> type any.

...first I fill lt_comp ...

linetype = cl_abap_structdescr=>create( lt_comp ).

tabletype = cl_abap_tabledescr=>create( p_line_type = linetype p_table_kind = 'S' ).

create data lr_table type handle tabletype.

assign lr_table->* to <lt_data> casting type handle tabletype.

create data lr_line like line of <lt_data>.

assign lr_line->* to <ls_data> casting type handle linetype.

... filling the table <lt_data> ....

loop at lt_data assigning <ls_data>.

... when I try to access a field of <ls_data>, I get the error '<ls_data> is no structure'.

endloop.

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
359

Hello Steven

If you are going for dynamic programming then you have to use "the full monty" meaning field-symbols for:

- itab

- structure AND

- fields


FIELD-SYMBOLS:
  <ld_fld>    TYPE any.

  LOOP AT <lt_data> ASSIGNING <ls_data>.
    UNASSIGN <ld_fld>.

    ASSIGN COMPONENT '<name of field  OR sy-index>' OF STRUCTURE <ls_data>
                                                                                TO <ld_fld>.
   IF ( <ld_fld> IS ASSIGNED ).
"   do something...
   ENDIF.
  ENDLOOP.
...

Regards,

Uwe

2 REPLIES 2
Read only

uwe_schieferstein
Active Contributor
0 Likes
360

Hello Steven

If you are going for dynamic programming then you have to use "the full monty" meaning field-symbols for:

- itab

- structure AND

- fields


FIELD-SYMBOLS:
  <ld_fld>    TYPE any.

  LOOP AT <lt_data> ASSIGNING <ls_data>.
    UNASSIGN <ld_fld>.

    ASSIGN COMPONENT '<name of field  OR sy-index>' OF STRUCTURE <ls_data>
                                                                                TO <ld_fld>.
   IF ( <ld_fld> IS ASSIGNED ).
"   do something...
   ENDIF.
  ENDLOOP.
...

Regards,

Uwe

Read only

0 Likes
359

Hey Uwe,

Thanks for your answer. This works for me.

Steven