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.
Other versions: 7.31 | 7.40 | 7.54
Source Code
DATA assign TYPE c LENGTH 1 VALUE '1'.
cl_demo_input=>request(
EXPORTING text = `ASSIGN statement (1 to 6)`
CHANGING field = assign ).
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.
WHEN OTHERS.
cl_demo_output=>display( 'Enter a number between 1 and 6' ).
RETURN.
ENDCASE.
cl_demo_output=>write( |sy-subrc: { sy-subrc }| ).
IF <word> IS ASSIGNED OR <int> IS ASSIGNED.
cl_demo_output=>write( 'Field symbol is assigned' ).
ENDIF.
cl_demo_output=>display( ).
Description
This example shows why use the addition INCREMENT
in the statement
ASSIGN should be used only if it is necessary access sequences of similar memory areas and that
the typing of the field symbol must match the way
casting_spec is specified. Any access which is not appropriate as shown in the example can produce the following behavior:
- The first
ASSIGN
statement returns the value 0 insy-subrc
.<word>
is assigned the contiguous memory area ofstruc-int1
andstruc-int2
and this is cast to the typec
.
- The second
ASSIGN
statement terminates with a runtime error since the data type ofstruc-word
does not match the typing of<int>
.
- The third
ASSIGN
statement terminates with a runtime error since the system tries to assign the componentstruc-stri
to<word>
, and as the structure is deep and the typing of<word>
is flat, no casting is possible.
- The fourth
ASSIGN
statement terminates with a runtime error since the system tries to assign the componentstruc-stri
to<int>
, and as the structure is deep and the typing of<int>
is flat, no casting is possible.
- The fifth and the sixth
ASSIGN
statement both return the value 4 in sy-subrc since the system tries to allocate memory area outside the structurestruc
specified afterRANGE
.