Assign structure to string variable

Problem: you want to assign structure to a string variable.

Solution: in a non-unicode system this is quite easy. Simply assign structure to a variable.

DATA:
  lv_string type string,
  ls_structure type sflight.

lv_string = ls_structure.


Unfortunately structure to string variable assignment is not so easy in a unicode system. It will work as shown above only if a structure contains fields of a character type only. If a structure contains an integer field, like presented above structure SFLIGHT, the assignment will not be possible.

To bypass this problem there has been a special class created called CL_ABAP_CONTAINER_UTILITIES. This class contains method called FILL_CONTAINER_C – this method will do exactly what we need.

DATA:
  lv_string type string,
  ls_structure type sflight.

CALL METHOD cl_abap_container_utilities=>fill_container_c
  EXPORTING
    im_value               = ls_structure
  IMPORTING
    ex_container           = lv_string
  EXCEPTIONS
    illegal_parameter_type = 1
    OTHERS                 = 2.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

Komentowanie jest zakończone.