Entering content frame

Background documentation Assignments to Field Symbols Locate the document in its SAP Library structure

Until now the ASSIGN statement made it possible to define addresses past field limits by specifying the offset or length. There was only a runtime error when addressing past the limits of the data segment. Cross-field accesses to the offset/length in an ASSIGN, for example, could be used to edit repeating groups.

With Unicode, however, problems occur since it is not possible to ensure that cross-field offset or length definitions can be interpreted as bytes or characters in an identical and meaningful manner in both a US and an NUS. For this reason, the ASSIGN statement was enhanced with the RANGE and INCREMENT additions while the CASTING addition now supports all variants of this statement. The RANGE addition is offered for all valid variants of ASSIGN and can be combined with the CASTING addition.

The RANGE addition

ASSIGN feld1  TO <fs> RANGE feld2.

This addition explicitly sets the range limits, making it possible to define addresses past field limits, for example to edit repeating groups with the ASSIGN INCREMENT statement.

  1. The field limits of field2 are used as the range for <fs>.
  2. In a UP, the limits specified by the RANGE definition must include the range limits that would otherwise result from the rules described above.
  3. If the memory area of field1 is not completely contained in field2, there is a catchable runtime error.
  4. Field field2, which defines the range, may also be deep. Repeating groups with deep types therefore can also be processed.

ASSIGN feld INCREMENT n TO <fs>.

The field symbol is incremented by n times the length of field, starting with the position defined by field.

First the range for the access is defined from the length of field and the INCREMENT definition of the range for the access as defined by ASSIGN fld+n*sizeof[field] (sizeof[fld]) TO <fs>. The addressed range must lie within the range limits. If it is not possible to make the assignment because the range limits were violated, SY-SUBRC is set to > 0 and the field symbol is not changed.

The range limits for ASSIGN field INCREMENT n TO <fs> are defined in exactly the same way as ASSIGN field TO <fs>. The definition of the INCREMENT therefore has no effect on the definition of the range limits.

Example

* Loop through an elementary field

   DATA: c(10) TYPE C VALUE 'abcdefghij'.
FIELD-SYMBOLS: <cf> TYPE C.

   ASSIGN c(2) TO <cf>.               "Range limits c =
    
   DO 5 TIMES.                        "Field limits of cf
     WRITE / <cf>.
     ASSIGN  <cf> INCREMENT 1 TO <c>. "Same limits <c>
*    ASSIGN  <c>+2 TO <cf>.           "As for ASSIGN INCREMENT
   ENDDO.

* Structured repeating group

TYPES: BEGIN OF comp,
         f1 type string,
         ...
       END OF comp.

DATA:  BEGIN OF stru,
         x1(1) TYPE x,
         k1    TYPE comp,
         k2    TYPE comp,
         k3    TYPE comp,
         k4    TYPE comp,
       END OF stru.

FIELD-SYMBOLS: <comp> TYPE comp.

ASSIGN stru-k1 TO <comp> RANGE stru.

* Specify that range limits are to exceed field boundaries

DO 4 TIMES.
  ...
  ASSIGN <comp> INCREMENT 1 TO <comp>.
ENDDO.

* Access an element of a repeating group dynamically

DATA: BEGIN OF stru,
        x1(1) TYPE x,
        k1    TYPE comp,
        k2    TYPE comp,
        k3    TYPE comp,
        k4    TYPE comp,
      END OF stru,

      incr    TYPE i.

FIELD-SYMBOLS: <comp> TYPE comp.

incr = 4 - 1.
ASSIGN stru-k1 INCREMENT incr TO <comp> RANGE stru.

...

* <comp> now points to stru-k4

The CASTING addition

The CASTING addition is allowed for all variants of the ASSIGN statement:

ASSIGN feld TO <fs> CASTING.
ASSIGN feld TO <fs> CASTING TYPE  type.
ASSIGN feld TO <fs> CASTING TYPE (typename)
ASSIGN feld TO <fs> CASTING LIKE  fld.
ASSIGN feld TO <fs> CASTING DECIMALS dec.

You can use ASSIGN ... CASTING to look at the contents of a field as a value of another type using a field symbol . One application for this statement would be to provide different views on a structure with casts on different types.

One wide-spread ABAP technique is to use C fields or structures as containers for storing structures of different types that are frequently only known at runtime. The components of the structure are selected with offset/length accesses to the container. Since this technique no longer works with Unicode, you can also look upon an existing memory area as a container with the suitable type definition using a field symbol with the ASSIGN ... CASTING statement. In the next example, a certain field of database table X031L is read, whereby the field and table names are only defined at runtime.

Example

* Read a field from the table X031L

PARAMETERS:
  TAB_NAME    LIKE SY-TNAME,           "Table name
  TAB_COMP    LIKE X031L-FIELDNAME,   "Field name
  ANZAHL      TYPE I DEFAULT 10.       Number of lines

DATA:
    BEGIN OF BUFFER,
       ALIGNMENT TYPE F,                   "Alignment
       C(8000)   TYPE C,                   "Table content
  END OF BUFFER.

FIELD-SYMBOLS: <WA>   TYPE ANY,
               <COMP> TYPE ANY.

* Set field symbol with appropriate type
* to buffer area

ASSIGN BUFFER TO <WA> CASTING TYPE (TAB_NAME).

SELECT * FROM (TAB_NAME) INTO <WA>
  UP TO anzahl ROWS.
  ASSIGN COMPONENT TAB_COMP OF STRUCTURE <WA> TO <COMP>.
  WRITE: / TAB_COMP, <COMP>.
ENDSELECT.

Note

Until now, in the ASSIGN field TO <fs> CASTING... statement, the system checked to ensure that field was at least as long as the type that was assigned to the field symbol, <fs>. (Field symbols can either be typed at declaration or the type specified in an ASSIGN statement using CASTING TYPE). The syntax check is now more thorough. Now, you can only assign the field feld provided it is at least as long in both the UP and the NUP as the type assigned to the field symbol <fs>. Otherwise, the system returns a syntax error. At runtime, the system only checks to see whether or not the lengths are compatible in the current system (as before).

If the field type or field symbol type is a deep structure, the system also checks that the offset and type of all the reference components match in the area of field that is covered by <fs>. The syntax check is now more thorough. Now, the system checks that these components must be compatible in all systems, whether they have a one-byte, double-byte, or four-byte character length. At runtime, the system only checks to see whether or not the reference components are compatible in the current system

In USs in the ASSIGN str TO <fs> TYPE C/N ASSIGN str TO <fs> CASTING TYPE C/N statements, the length of str may not always be a multiple of the character length, in which case the program aborts at runtime.

 

Note Use the categories used in the Dictionary when using these types with Structure Enhancements.

Leaving content frame