Show TOC

Assigning Components of Structures to a Field SymbolLocate this document in the navigation structure

For a structured data object struc, you can use the statement

ASSIGN COMPONENT comp OF STRUCTURE struc TO <fs>.

to assign one of its components comp to the field symbol <fs>. You can specify the component compeither as a literal or a variable. If comp is of type c or a structure which has no internal tables as components, it specifies the name of the component. If comp has any other elementary data type, it is converted to type i and specifies the number of the component. If the assignment is successful, sy-subrc is set to 0. Otherwise, it returns 4.

This statement is particularly important for addressing components of structured data objects dynamically. If you assign a data object to a field symbol generically, or using Casting , or pass it generically to the parameter interface of a procedure, you cannot address its components either statically or dynamically. Instead, you must use the above statement. This allows indirect access either using the component name or its index number.

Tip

REPORT demo_field_symbols_stat_assign .

DATA: BEGIN OF line,        col1 TYPE i VALUE 11,        col2 TYPE i VALUE 22,        col3 TYPE i VALUE 33,      END OF line.

DATA comp(5) TYPE c VALUE 'COL3'.

FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE ANY, <f3> TYPE ANY.

ASSIGN line TO <f1>.ASSIGN comp TO <f2>.

 DO 3 TIMES.  ASSIGN COMPONENT sy-index OF STRUCTURE <f1> TO <f3>.  WRITE <f3>.  ENDDO.

ASSIGN COMPONENT <f2> OF STRUCTURE <f1> TO <f3>.WRITE / <f3>.

The list output is:

11         22         33

33

The field symbol <f1> points to the structure line, <f2>points to the field comp. In the DO loop , the components of line are specified by their numbers and assigned one by one to <f3>. After the loop, the components col3are specified by their names and assigned to <f3>. Note that ASSIGN COMPONENT is the only possible method of addressing the components of <f1>. Expressions such as <f1>-col1 are syntactically incorrect.