Ansicht
Dokumentation

ABAPASSIGN_DYNAMIC_COMPONENTS - ASSIGN DYNAMIC COMPONENTS

ABAPASSIGN_DYNAMIC_COMPONENTS - ASSIGN DYNAMIC COMPONENTS

BAL_S_LOG - Application Log: Log header data   Vendor Master (General Section)  
This documentation is copyright by SAP AG.
SAP E-Book

ASSIGN, dynamic_components

Short Reference



... ${ 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 alternatives for specifying the memory area mem_area of the statement ASSIGN access components of structures dynamically.

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 struc it is detected at runtime that struc is not a structure, 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:

  • If comp is character-like, its content is interpreted as the name of the component. The name is not case-sensitive. It may contain offsets and lengths, structure component selectors, and component selectors.
  • If comp is of type i, its value is 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 the specified component is not found, sy-subrc is set to 4.

Example

Two dynamic assignments with dynamic specification of components:

  • in the first, the structure is specified directly and the component is specified by its position.
  • in the second, the structure is specified by a writable expression with constructor operator NEW and the component is specified by its name, which can have an offset/length specification.



Alternative 2

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

Note

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.

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 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, sy-subrc is set to 4.

Notes

  • 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.

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 downcast 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.

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

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

Field Symbols, Dynamic Structure Components






CL_GUI_FRONTEND_SERVICES - Frontend Services   Fill RESBD Structure from EBP Component Structure  
This documentation is copyright by SAP AG.

Length: 9725 Date: 20240614 Time: 032605     sap01-206 ( 154 ms )