본문 바로가기

SAP/ABAP

[ABAP] ASSIGN COMPONENT

반응형

ASSIGN, dynamic_components



Syntax

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


Alternatives:

1. ... dref->(comp_name)

2. ... COMPONENT comp OF STRUCTURE struc

Effect

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

Alternative 1

... dref->(comp_name)


Effect

This form access 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.

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.

DATA(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.

Alternative 2

... COMPONENT comp OF STRUCTURE struc


Effect

This expression for mem_area assigns a 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 struc is specified as an expression, its result must be structured. If struc is specified as a data object, it does not need to be structured.

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

  • If the field comp has a text-like type (c or string) or the type of a flat structure, which exclusively contains character-like components, its content is interpreted as the name of the component. The name must be in uppercase letters. It may contain offsets and lengths, structure component selectors, and component selectors for assigning structured data objects and attributes in classes or objects.
  • If the field comp has a non-text-like elementary type, the content is converted to the type i and interpreted as the position of the component in the structure. If the value of comp is 0, the memory area of the entire structure is assigned to the field symbol.
  • If comp has a different type, a syntax error or runtime error occurs.

If an operand struc specified as a data object is not a structure or the specified component is not found, the assignment is not made and sy-subrc is set to 4.

Hints

  • Identifying a component by its name is far less efficient than using its position, since far more internal processes are involved. Using COMPONENTS OF, however, is always more efficient than specifying the name after the structure component selector within a fully dynamically specified component in a parenthesized data object name (see this executable example).
  • If the structure struc is specified as a table expression and the corresponding line is not found, the exception CX_SY_ITAB_LINE_NOT_FOUND is raised.
  • Writable expressions can be specified for struc but no other expressions, since only these can have a non-temporary result. Assigning a component of a temporary structure to a field symbol would not make sense.
  • If struc is specified, it is advisable to specify only structures as a data object and to check this in advance. Just evaluating sy-subrc is not enough to determine why an assignment was not successful.



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  @DATA(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>.

  • The first implementation does not use RTTI. The statement DESCRIBE FIELD is used to check whether the passed data object is a structure. The components are then assigned one after another to the field symbol in a DO loop.
  • The second implementation uses RTTI. A down cast of the type description object to the class CL_ABAP_STRUCTDESCR for the passed data object ensures that the object is a structure. A loop across the component table COMPONENTS assigns the components to the field symbol via their names.

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 DATA(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.
        DATA(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> ).

Syntax

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


Alternatives:

1. ... dref->(comp_name)

2. ... COMPONENT comp OF STRUCTURE struc

Effect

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

Alternative 1

... dref->(comp_name)


Effect

This form access 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.

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.

DATA(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.

Alternative 2

... COMPONENT comp OF STRUCTURE struc


Effect

This expression for mem_area assigns a 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 struc is specified as an expression, its result must be structured. If struc is specified as a data object, it does not need to be structured.

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

  • If the field comp has a text-like type (c or string) or the type of a flat structure, which exclusively contains character-like components, its content is interpreted as the name of the component. The name must be in uppercase letters. It may contain offsets and lengths, structure component selectors, and component selectors for assigning structured data objects and attributes in classes or objects.
  • If the field comp has a non-text-like elementary type, the content is converted to the type i and interpreted as the position of the component in the structure. If the value of comp is 0, the memory area of the entire structure is assigned to the field symbol.
  • If comp has a different type, a syntax error or runtime error occurs.

If an operand struc specified as a data object is not a structure or the specified component is not found, the assignment is not made and sy-subrc is set to 4.

Hints

  • Identifying a component by its name is far less efficient than using its position, since far more internal processes are involved. Using COMPONENTS OF, however, is always more efficient than specifying the name after the structure component selector within a fully dynamically specified component in a parenthesized data object name (see this executable example).
  • If the structure struc is specified as a table expression and the corresponding line is not found, the exception CX_SY_ITAB_LINE_NOT_FOUND is raised.
  • Writable expressions can be specified for struc but no other expressions, since only these can have a non-temporary result. Assigning a component of a temporary structure to a field symbol would not make sense.
  • If struc is specified, it is advisable to specify only structures as a data object and to check this in advance. Just evaluating sy-subrc is not enough to determine why an assignment was not successful.



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  @DATA(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>.

  • The first implementation does not use RTTI. The statement DESCRIBE FIELD is used to check whether the passed data object is a structure. The components are then assigned one after another to the field symbol in a DO loop.
  • The second implementation uses RTTI. A down cast of the type description object to the class CL_ABAP_STRUCTDESCR for the passed data object ensures that the object is a structure. A loop across the component table COMPONENTS assigns the components to the field symbol via their names.

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 DATA(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.
        DATA(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> ).

Syntax

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


Alternatives:

1. ... dref->(comp_name)

2. ... COMPONENT comp OF STRUCTURE struc

Effect

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

Alternative 1

... dref->(comp_name)


Effect

This form access 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.

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.

DATA(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.

Alternative 2

... COMPONENT comp OF STRUCTURE struc


Effect

This expression for mem_area assigns a 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 struc is specified as an expression, its result must be structured. If struc is specified as a data object, it does not need to be structured.

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

  • If the field comp has a text-like type (c or string) or the type of a flat structure, which exclusively contains character-like components, its content is interpreted as the name of the component. The name must be in uppercase letters. It may contain offsets and lengths, structure component selectors, and component selectors for assigning structured data objects and attributes in classes or objects.
  • If the field comp has a non-text-like elementary type, the content is converted to the type i and interpreted as the position of the component in the structure. If the value of comp is 0, the memory area of the entire structure is assigned to the field symbol.
  • If comp has a different type, a syntax error or runtime error occurs.

If an operand struc specified as a data object is not a structure or the specified component is not found, the assignment is not made and sy-subrc is set to 4.

Hints

  • Identifying a component by its name is far less efficient than using its position, since far more internal processes are involved. Using COMPONENTS OF, however, is always more efficient than specifying the name after the structure component selector within a fully dynamically specified component in a parenthesized data object name (see this executable example).
  • If the structure struc is specified as a table expression and the corresponding line is not found, the exception CX_SY_ITAB_LINE_NOT_FOUND is raised.
  • Writable expressions can be specified for struc but no other expressions, since only these can have a non-temporary result. Assigning a component of a temporary structure to a field symbol would not make sense.
  • If struc is specified, it is advisable to specify only structures as a data object and to check this in advance. Just evaluating sy-subrc is not enough to determine why an assignment was not successful.



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  @DATA(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>.

  • The first implementation does not use RTTI. The statement DESCRIBE FIELD is used to check whether the passed data object is a structure. The components are then assigned one after another to the field symbol in a DO loop.
  • The second implementation uses RTTI. A down cast of the type description object to the class CL_ABAP_STRUCTDESCR for the passed data object ensures that the object is a structure. A loop across the component table COMPONENTS assigns the components to the field symbol via their names.

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 DATA(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.
        DATA(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> ).

Syntax

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


Alternatives:

1. ... dref->(comp_name)

2. ... COMPONENT comp OF STRUCTURE struc

Effect

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

Alternative 1

... dref->(comp_name)


Effect

This form access 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.

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.

DATA(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.

Alternative 2

... COMPONENT comp OF STRUCTURE struc


Effect

This expression for mem_area assigns a 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 struc is specified as an expression, its result must be structured. If struc is specified as a data object, it does not need to be structured.

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

  • If the field comp has a text-like type (c or string) or the type of a flat structure, which exclusively contains character-like components, its content is interpreted as the name of the component. The name must be in uppercase letters. It may contain offsets and lengths, structure component selectors, and component selectors for assigning structured data objects and attributes in classes or objects.
  • If the field comp has a non-text-like elementary type, the content is converted to the type i and interpreted as the position of the component in the structure. If the value of comp is 0, the memory area of the entire structure is assigned to the field symbol.
  • If comp has a different type, a syntax error or runtime error occurs.

If an operand struc specified as a data object is not a structure or the specified component is not found, the assignment is not made and sy-subrc is set to 4.

Hints

  • Identifying a component by its name is far less efficient than using its position, since far more internal processes are involved. Using COMPONENTS OF, however, is always more efficient than specifying the name after the structure component selector within a fully dynamically specified component in a parenthesized data object name (see this executable example).
  • If the structure struc is specified as a table expression and the corresponding line is not found, the exception CX_SY_ITAB_LINE_NOT_FOUND is raised.
  • Writable expressions can be specified for struc but no other expressions, since only these can have a non-temporary result. Assigning a component of a temporary structure to a field symbol would not make sense.
  • If struc is specified, it is advisable to specify only structures as a data object and to check this in advance. Just evaluating sy-subrc is not enough to determine why an assignment was not successful.



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  @DATA(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>.

  • The first implementation does not use RTTI. The statement DESCRIBE FIELD is used to check whether the passed data object is a structure. The components are then assigned one after another to the field symbol in a DO loop.
  • The second implementation uses RTTI. A down cast of the type description object to the class CL_ABAP_STRUCTDESCR for the passed data object ensures that the object is a structure. A loop across the component table COMPONENTS assigns the components to the field symbol via their names.

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 DATA(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.
        DATA(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> ).

Syntax

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


Alternatives:

1. ... dref->(comp_name)

2. ... COMPONENT comp OF STRUCTURE struc

Effect

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

Alternative 1

... dref->(comp_name)


Effect

This form access 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.

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.

DATA(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.

Alternative 2

... COMPONENT comp OF STRUCTURE struc


Effect

This expression for mem_area assigns a 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 struc is specified as an expression, its result must be structured. If struc is specified as a data object, it does not need to be structured.

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

  • If the field comp has a text-like type (c or string) or the type of a flat structure, which exclusively contains character-like components, its content is interpreted as the name of the component. The name must be in uppercase letters. It may contain offsets and lengths, structure component selectors, and component selectors for assigning structured data objects and attributes in classes or objects.
  • If the field comp has a non-text-like elementary type, the content is converted to the type i and interpreted as the position of the component in the structure. If the value of comp is 0, the memory area of the entire structure is assigned to the field symbol.
  • If comp has a different type, a syntax error or runtime error occurs.

If an operand struc specified as a data object is not a structure or the specified component is not found, the assignment is not made and sy-subrc is set to 4.

Hints

  • Identifying a component by its name is far less efficient than using its position, since far more internal processes are involved. Using COMPONENTS OF, however, is always more efficient than specifying the name after the structure component selector within a fully dynamically specified component in a parenthesized data object name (see this executable example).
  • If the structure struc is specified as a table expression and the corresponding line is not found, the exception CX_SY_ITAB_LINE_NOT_FOUND is raised.
  • Writable expressions can be specified for struc but no other expressions, since only these can have a non-temporary result. Assigning a component of a temporary structure to a field symbol would not make sense.
  • If struc is specified, it is advisable to specify only structures as a data object and to check this in advance. Just evaluating sy-subrc is not enough to determine why an assignment was not successful.



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  @DATA(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>.

  • The first implementation does not use RTTI. The statement DESCRIBE FIELD is used to check whether the passed data object is a structure. The components are then assigned one after another to the field symbol in a DO loop.
  • The second implementation uses RTTI. A down cast of the type description object to the class CL_ABAP_STRUCTDESCR for the passed data object ensures that the object is a structure. A loop across the component table COMPONENTS assigns the components to the field symbol via their names.

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 DATA(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.
        DATA(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> ).

Syntax

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


Alternatives:

1. ... dref->(comp_name)

2. ... COMPONENT comp OF STRUCTURE struc

Effect

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

Alternative 1

... dref->(comp_name)


Effect

This form access 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.

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.

DATA(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.

Alternative 2

... COMPONENT comp OF STRUCTURE struc


Effect

This expression for mem_area assigns a 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 struc is specified as an expression, its result must be structured. If struc is specified as a data object, it does not need to be structured.

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

  • If the field comp has a text-like type (c or string) or the type of a flat structure, which exclusively contains character-like components, its content is interpreted as the name of the component. The name must be in uppercase letters. It may contain offsets and lengths, structure component selectors, and component selectors for assigning structured data objects and attributes in classes or objects.
  • If the field comp has a non-text-like elementary type, the content is converted to the type i and interpreted as the position of the component in the structure. If the value of comp is 0, the memory area of the entire structure is assigned to the field symbol.
  • If comp has a different type, a syntax error or runtime error occurs.

If an operand struc specified as a data object is not a structure or the specified component is not found, the assignment is not made and sy-subrc is set to 4.

Hints

  • Identifying a component by its name is far less efficient than using its position, since far more internal processes are involved. Using COMPONENTS OF, however, is always more efficient than specifying the name after the structure component selector within a fully dynamically specified component in a parenthesized data object name (see this executable example).
  • If the structure struc is specified as a table expression and the corresponding line is not found, the exception CX_SY_ITAB_LINE_NOT_FOUND is raised.
  • Writable expressions can be specified for struc but no other expressions, since only these can have a non-temporary result. Assigning a component of a temporary structure to a field symbol would not make sense.
  • If struc is specified, it is advisable to specify only structures as a data object and to check this in advance. Just evaluating sy-subrc is not enough to determine why an assignment was not successful.



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  @DATA(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>.

  • The first implementation does not use RTTI. The statement DESCRIBE FIELD is used to check whether the passed data object is a structure. The components are then assigned one after another to the field symbol in a DO loop.
  • The second implementation uses RTTI. A down cast of the type description object to the class CL_ABAP_STRUCTDESCR for the passed data object ensures that the object is a structure. A loop across the component table COMPONENTS assigns the components to the field symbol via their names.

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 DATA(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.
        DATA(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> ).

Syntax

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


Alternatives:

1. ... dref->(comp_name)

2. ... COMPONENT comp OF STRUCTURE struc

Effect

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

Alternative 1

... dref->(comp_name)


Effect

This form access 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.

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.

DATA(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.

Alternative 2

... COMPONENT comp OF STRUCTURE struc


Effect

This expression for mem_area assigns a 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 struc is specified as an expression, its result must be structured. If struc is specified as a data object, it does not need to be structured.

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

  • If the field comp has a text-like type (c or string) or the type of a flat structure, which exclusively contains character-like components, its content is interpreted as the name of the component. The name must be in uppercase letters. It may contain offsets and lengths, structure component selectors, and component selectors for assigning structured data objects and attributes in classes or objects.
  • If the field comp has a non-text-like elementary type, the content is converted to the type i and interpreted as the position of the component in the structure. If the value of comp is 0, the memory area of the entire structure is assigned to the field symbol.
  • If comp has a different type, a syntax error or runtime error occurs.

If an operand struc specified as a data object is not a structure or the specified component is not found, the assignment is not made and sy-subrc is set to 4.

Hints

  • Identifying a component by its name is far less efficient than using its position, since far more internal processes are involved. Using COMPONENTS OF, however, is always more efficient than specifying the name after the structure component selector within a fully dynamically specified component in a parenthesized data object name (see this executable example).
  • If the structure struc is specified as a table expression and the corresponding line is not found, the exception CX_SY_ITAB_LINE_NOT_FOUND is raised.
  • Writable expressions can be specified for struc but no other expressions, since only these can have a non-temporary result. Assigning a component of a temporary structure to a field symbol would not make sense.
  • If struc is specified, it is advisable to specify only structures as a data object and to check this in advance. Just evaluating sy-subrc is not enough to determine why an assignment was not successful.



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  @DATA(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>.

  • The first implementation does not use RTTI. The statement DESCRIBE FIELD is used to check whether the passed data object is a structure. The components are then assigned one after another to the field symbol in a DO loop.
  • The second implementation uses RTTI. A down cast of the type description object to the class CL_ABAP_STRUCTDESCR for the passed data object ensures that the object is a structure. A loop across the component table COMPONENTS assigns the components to the field symbol via their names.

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 DATA(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.
        DATA(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> ).

반응형