Application Development 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: 

filling up members of a structure with some default value

Former Member
0 Kudos
1,513

Hi,

I have a data like:

DATA:

ls_abc_lane TYPE bapi11201tr2.

here, ls_abc_lane contains many fields and i want to set each of them value as 'X'.

Traditional way is to fill up like:

ls_abc_lane-field1 = 'X'.

ls_abc_lane-field2 = 'X'.

..etc.

BUT, if there are many fields it is time consuming to type/copy paste for each field. If there any way to loop through the fields and set up the 'X' value ?

Points will be given to the helpful answer.

Thanks and regards,

Ramesh.

5 REPLIES 5

Former Member
0 Kudos
220

Try this:

data: v_col_index type i.

FIELD-SYMBOLS: <wa> TYPE bapi11201tr2

<FS_component> TYPE ANY.

v_col_index = 1.

assign ls_abc_lane to <wa>.

do.

ASSIGN COMPONENT v_col_index OF STRUCTURE <wa> TO <FS_component>.

if sy-subrc ne 0.

exit.

endif.

<FS_component> = 'X'.

enddo.

Former Member
0 Kudos
220

I don't have that structure defined in my system, so I used a different one.

DATA:
  ls_abc_lane TYPE tstc.

FIELD-SYMBOLS:
  <fs_component> TYPE ANY.


DO.
  ASSIGN COMPONENT sy-index
    OF STRUCTURE ls_abc_lane TO <fs_component>.

  <fs_component> = 'X'.

  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
ENDDO.

0 Kudos
220

Thanks for the helpful answers.

The above solution fails if the <fs_componet> is other than the data type 'CHAR'. It throws exception saying 'X' can not be converted into decimal etc.

How do we check if the component is of only type CHAR then set value to 'X'.?

Thanks

Ramesh.

0 Kudos
220

You could use the RTTS classes:

DATA:
    ls_abc_lane      TYPE tstc,
    l_r_typedescr    TYPE REF TO cl_abap_typedescr.


FIELD-SYMBOLS:
  <fs_component> TYPE ANY.


DO.
  ASSIGN COMPONENT sy-index
    OF STRUCTURE ls_abc_lane TO <fs_component>.

  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

  CALL METHOD cl_abap_typedescr=>describe_by_data
    EXPORTING
      p_data      = <fs_component>
    RECEIVING
      p_descr_ref = l_r_typedescr.

  IF l_r_typedescr->type_kind = 'C'. " or others.
    <fs_component> = 'X'.
  ENDIF.
ENDDO.

Edited by: Jerry Coleman on Jan 20, 2009 8:37 AM

0 Kudos
220

Hi,

The above reply helped me to solve the issue.

Thank you very much.

Kind regards,

Ramesh.