SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Assignments → Assigning References → Setting Field Symbols → ASSIGN → ASSIGN - mem_area → ASSIGN - dynamic_dobj →Field Symbols, ASSIGN INCREMENT
The examples shows how the statement ASSIGN behaves when the addition INCREMENT is used.
Source Code
REPORT demo_assign_increment.
PARAMETERS assign TYPE c LENGTH 1 DEFAULT '1'.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
DATA: BEGIN OF struc,
word TYPE c LENGTH 4 VALUE 'abcd',
int1 TYPE i VALUE 111,
int2 TYPE i VALUE 222,
stri TYPE string VALUE `efgh`,
END OF struc.
FIELD-SYMBOLS: <word> LIKE struc-word,
<int> TYPE i.
CASE assign.
WHEN '1'. "-> sy-subrc 0
ASSIGN struc-word INCREMENT 1 TO <word> RANGE struc.
WHEN '2'. "-> Runtime error
ASSIGN struc-word INCREMENT 1 TO <int> RANGE struc.
WHEN '3'. "-> Runtime error
ASSIGN struc-word INCREMENT 2 TO <word> RANGE struc.
WHEN '4'. "-> Runtime error
ASSIGN struc-word INCREMENT 2 TO <int> RANGE struc.
WHEN '5'. "-> sy-subrc 4
ASSIGN struc-word INCREMENT 3 TO <word> RANGE struc.
WHEN '6'. "-> sy-subrc 4
ASSIGN struc-word INCREMENT 3 TO <int> RANGE struc.
ENDCASE.
WRITE: / 'sy-subrc:', sy-subrc.
IF <word> IS ASSIGNED OR <int> IS ASSIGNED.
WRITE / 'Field symbol is assigned'.
ENDIF.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
AT SELECTION-SCREEN.
IF assign NOT BETWEEN '1' AND '6'.
MESSAGE 'Enter a number between 1 and 6' TYPE 'E'.
ENDIF.
Description
This example shows why you should use the addition INCREMENT in the statement ASSIGN only if you want to access sequences of similar memory areas and that the typing of the field symbol must match the specification made in casting_spec. Any access which is not appropriate as shown in the example can produce the following behavior: