Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Assignments →  Assigning References →  Setting Field Symbols →  ASSIGN →  ASSIGN - mem_area →  ASSIGN - dynamic_dobj 

Field Symbols, Dynamic Structure Components

This example demonstrates dynamic assignments of structure components using ASSIGN.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA:
      BEGIN OF struc,
        comp1 TYPE i,
        comp2 TYPE i,
        comp3 TYPE i,
      END OF struc,
      name TYPE string,
      t1 TYPE i,
      t2 TYPE i,
      tr TYPE i.
    FIELD-SYMBOLS: <struc> TYPE any,
                   <comp>  TYPE any.

    ASSIGN struc TO <struc>.

    name = `<STRUC>-COMP1`.
    GET RUN TIME FIELD t1.
    DO 1000 TIMES.
      ASSIGN (name) TO <comp>.
    ENDDO.
    GET RUN TIME FIELD t2.
    tr = t2 - t1.
    cl_demo_output=>write( |{ tr }| ).

    name = `COMP1`.
    GET RUN TIME FIELD t1.
    DO 1000 TIMES.
      ASSIGN COMPONENT name OF STRUCTURE <struc> TO <comp>.
    ENDDO.
    GET RUN TIME FIELD t2.
    tr = t2 - t1.
    cl_demo_output=>write( |{ tr }| ).

    GET RUN TIME FIELD t1.
    DO 1000 TIMES.
      ASSIGN COMPONENT 1 OF STRUCTURE <struc> TO <comp>.
    ENDDO.
    GET RUN TIME FIELD t2.
    tr = t2 - t1.
    cl_demo_output=>display( |{ tr }| ).

Description

A structure is assigned to a generically typed field symbol. The field symbol can only be used to access individual components dynamically. This example shows three different methods and measures their runtime. A fully dynamic access is the most performance-intensive. An access using ASSIGN COMPONENT is better for performance, and using the position is better than using the name.