ABAP - Keyword Documentation →  ABAP - Programming Language →  Processing Internal Data →  Assignments →  Assigning References →  Assigning Field Symbols →  ASSIGN →  ASSIGN, mem_area → 
Mail Feedback

ASSIGN, dynamic_components

Short Reference

Syntax

... { struc-(comp) }
  | { dref->(comp_name) }
  | { COMPONENT comp OF STRUCTURE struc } ...

Alternatives:

1. ... struc-(comp)

2. ... dref->(comp_name)

3. ... COMPONENT comp OF STRUCTURE struc

Effect

These variants for specifying the memory area mem_area of the statement ASSIGN access components of structures dynamically.

In these variants, the statement ASSIGN sets the return code sy-subrc. If the assignment is successful, sy-subrc is set to 0. In these variants, also exceptions can occur in case of some invalid dynamic specifications. If the assignment is not successful and no exception occurs, sy-subrc is set to 4. If sy-subrc is set to 4, the state of the field symbol depends on the addition ELSE UNASSIGN:

Hint

If ELSE UNASSIGN is not specified, it is not sufficient to evaluate the predicate expression <fs> IS ASSIGNED but sy-subrc must be checked. If ELSE UNASSIGN is specified, the predicate expression as well as sy-subrc can be evaluated.

Alternative 1  

... struc-(comp)


Effect

This variant of mem_area assigns the memory area of a component specified in comp of a structure struc to the field symbol. struc is a result position. The structure can be specified as a data object or as a writable expression. If it is detected at compile time that struc is not a structure, a syntax error occurs. If that is detected at runtime, the runtime error STRUCTURE_ILLEGAL occurs.

For comp, either a character-like data object or a numeric data object of type i can be specified:

Example

Two dynamic assignments with dynamic specification of components:

CLASS cls DEFINITION.
  PUBLIC SECTION.
    TYPES:
      BEGIN OF struct,
        col TYPE scarr,
      END OF struct.
    DATA attr TYPE struct.
ENDCLASS.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    DATA struct TYPE scarr.
    METHODS meth.
ENDCLASS.
CLASS demo IMPLEMENTATION.
  METHOD meth.
    DATA(comp1) = 1.
    cl_demo_input=>add_field( CHANGING field = comp1 ).
    DATA(comp2) = `col-carrid(2)`.
    cl_demo_input=>request( CHANGING field = comp2 ).

    ASSIGN struct-(comp1) TO FIELD-SYMBOL(<fs1>).
    cl_demo_output=>write( sy-subrc ).

    ASSIGN NEW cls( )->attr-(comp2) TO FIELD-SYMBOL(<fs2>).
    cl_demo_output=>write( sy-subrc ).

    cl_demo_output=>display( ).
  ENDMETHOD.
ENDCLASS.

Executable Example

Field Symbols, Dynamic Structure Components



Alternative 2  

... dref->(comp_name)


Effect

This variant accesses components of structures that are referenced by a data reference variable dref. dref can be any data reference variable that points to structure that contains the component specified in a character-like field comp_name.

The component name does not have to be in uppercase letters. It can contain offsets/lengths, structure component selectors, object component selectors, and class component selectors, in order to assign parts of the component or referenced objects of the component.

Unlike all other operand positions, where a data reference that does not point to a data object raises an exception, no exception occurs in the statement ASSIGN and sy-subrc is set to 4.

Hint

This syntax form corresponds to dynamic access to object components.

Example

Dynamic assignment of a component of a structure to a field symbol. The default value of comp_name denotes a substring of a component of a substructure.

DATA:
  BEGIN OF struc,
    BEGIN OF substruc,
      col TYPE c LENGTH 10 VALUE 'abcdefghij',
    END OF substruc,
  END OF struc.

FINAL(dref) = REF #( struc ).

DATA(comp_name) = `substruc-col+2(5)`.
cl_demo_input=>request( CHANGING field = comp_name ).

ASSIGN dref->(comp_name) TO FIELD-SYMBOL(<fs>).

IF sy-subrc = 0.
  cl_demo_output=>display( <fs> ).
ELSE.
  cl_demo_output=>display( 'Wrong specification' ).
ENDIF.

Example

Dynamic assignment of a component of a structure with an initial reference variable. No exception occurs but sy-subrc is set to 0 in this case.

DATA dref TYPE REF TO scarr.

ASSIGN dref->('carrid') TO FIELD-SYMBOL(<carrid1>).
ASSERT sy-subrc = 4.

dref = NEW #( ).
ASSIGN dref->('carrid') TO FIELD-SYMBOL(<carrid2>).
ASSERT sy-subrc = 0.

Alternative 3  

... COMPONENT comp OF STRUCTURE struc


Effect

This variant of mem_area assigns the memory area of a component comp of a structure struc to the field symbol.

struc is a result position. The structure can be specified as a data object or as a writable expression. If it is detected at compile time, that struc is not a structure:

If it is detected at runtime that struc is not a structure, in both cases, sy-subrc is set to 4 and the state of the field symbol depends on addition ELSE UNASSIGN.

comp is a character-like or numeric expression position. The evaluation depends on the data type of comp:



Hints



Example

Assignment of all components of a structure to a field symbol in a loop. In every loop pass, the component is assigned whose position is determined by the loop index.

SELECT SINGLE *
       FROM scarr
       WHERE carrid = 'UA'
       INTO @FINAL(wa).

DO.
  ASSIGN COMPONENT sy-index OF STRUCTURE wa TO FIELD-SYMBOL(<fs>).
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
  cl_demo_output=>write( <fs> ).
ENDDO.
cl_demo_output=>display( ).

Example

The following two methods show the dynamic assignment of the components of a structure that is passed to the parameter para of the methods to a field symbol <comp>.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    METHODS: meth1 IMPORTING para TYPE data,
             meth2 IMPORTING para TYPE data.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD meth1.
    DESCRIBE FIELD para TYPE FINAL(dtype).
    IF dtype <> 'u' AND dtype <> 'v'.
      RETURN.
    ENDIF.
    DO.
      ASSIGN COMPONENT sy-index
        OF STRUCTURE para TO FIELD-SYMBOL(<comp>).
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      ...
    ENDDO.
  ENDMETHOD.
  METHOD meth2.
    TRY.
        FINAL(struct_descr) = CAST cl_abap_structdescr(
          cl_abap_typedescr=>describe_by_data( para ) ).
      CATCH cx_sy_move_cast_error.
        RETURN.
    ENDTRY.
    LOOP AT struct_descr->components
            ASSIGNING FIELD-SYMBOL(<comp_descr>).
      ASSIGN COMPONENT <comp_descr>-name
             OF STRUCTURE para TO FIELD-SYMBOL(<comp>).
      ...
    ENDLOOP.
  ENDMETHOD.
ENDCLASS.

Example

Assignment of a component of a line of an internal table to a field symbol.

TYPES:
  BEGIN OF struc,
    col1 TYPE i,
    col2 TYPE i,
  END OF struc.

DATA itab TYPE STANDARD TABLE OF struc WITH EMPTY KEY.

itab = VALUE #( ( col1 = 1 col2 = 2 )
                ( col1 = 3 col2 = 4 ) ).

ASSIGN COMPONENT 2 OF STRUCTURE itab[ 2 ] TO FIELD-SYMBOL(<fs>).

cl_demo_output=>display( <fs> ).

Example

Trying to assign a component of data object that is not a structure to a field symbol. This is detected only at runtime in this variant. sy-subrc is set to 4 and using ELSE UNASSIGN the field symbol that was assigned before is unassigned.

FINAL(field) = 111.

ASSIGN field TO  FIELD-SYMBOL(<fs>).
ASSERT <fs> IS ASSIGNED.

ASSIGN COMPONENT 1 OF STRUCTURE field TO <fs> ELSE UNASSIGN.
IF sy-subrc <> 0.
  ASSERT <fs> IS NOT ASSIGNED.
ENDIF.



Continue
Example Field Symbols, Dynamic Structure Components