ABAP for Cloud Development, ©Copyright 2024 SAP SE. All rights reserved.
ABAP - Keyword Documentation → ABAP - Programming Language → Processing Internal Data → Assignments → Assigning References → Assigning Field Symbols → ASSIGN →
ASSIGN, casting_spec
Syntax
... { }
| { CASTING { { }
| {TYPE type|(name)}
| {LIKE dobj}
| {[TYPE p] DECIMALS dec} } }
| { obsolete_casting } ...
Description
The specification casting_spec defines the data type used to handle the memory area mem_area assigned to the field symbol when a statement contains the field symbol in an operand position. Either the addition CASTING can be specified or nothing at all.
The following restrictions apply:
... { }
Description
If nothing is specified for casting_spec, the field symbol inherits the data
type of the data object used in mem_area and the assigned memory area is handled accordingly. This data type must
match the typing of the field symbol.
Example
After the first assignment the field symbol has type c of length 3 and after the second assignment the field symbol has type string. The statement DESCRIBE FIELD returns the corresponding values C 3 and g.
FIELD-SYMBOLS <fs> TYPE csequence.
ASSIGN 'xxx' TO <fs>.
DATA(type) = cl_abap_typedescr=>describe_by_data( <fs> )->type_kind.
cl_demo_output=>write( type ).
ASSIGN `xxx` TO <fs>.
type = cl_abap_typedescr=>describe_by_data( <fs> )->type_kind.
cl_demo_output=>display( type ).
... CASTING ...
1. ... { }
2. ... TYPE type|(name)
3. ... LIKE dobj
4. ... [TYPE p] DECIMALS dec
Description
If the addition CASTING is used in casting_spec, the memory area is handled as if it had the type specified by CASTING.
Casting can either take place implicitly using the typing of the field symbol or explicitly using one
of the additions TYPE, LIKE, or DECIMALS. In explicit castings, the field symbol can only be typed generically, not completely.
Hints
... { }
Description
If the addition CASTING is specified without further additions, the assigned
memory area is cast to the type of the field symbol. The field symbol must be either completely typed
or with one of the generic built-in ABAP types c, n, p, or x.
Example
Casting of the integer 333 as a byte field. Depending on the byte order, the output is either 4D01 or 014D.
TYPES hex4 TYPE x LENGTH 2.
FIELD-SYMBOLS <fs> TYPE hex4.
ASSIGN 333 TO <fs> CASTING.
cl_demo_output=>display( <fs> ).
... TYPE type|(name)
Description
Explicit specification of a data type after TYPE. The name of the type can be specified as follows:
Direct static specification as type.
Specification as the content of a character-like data object name that contains the name of a data type in uppercase letters when the statement is executed. The following can be specified for name:
If the data object name is specified as a character literal or as a constant, it can be evaluated statically, and the specified type is recognized as the used object.
If the data object name is specified as a variable, it is specified only dynamically, and the content is not evaluated statically.
When the statement is executed, name is not evaluated until runtime in both cases.
The assigned memory area is cast to the specified type. The data type specified after TYPE cannot be generic, apart from the built-in ABAP types c, n, p, and x. Furthermore, table categories and REF TO cannot be specified.
The field symbol <fs> can only be typed generically and not completely. The specified data type must match the generic typing of the field symbol, meaning that castings are allowed to specialize the generic typing but not to make it more general.
If a generic character-like type c or n is specified after TYPE, the length of the assigned memory area must be a multiple of the length of a character in the memory when the statement is executed.
Example
Casting of a time field to a structured type with three components.
TYPES:
BEGIN OF time,
hours TYPE c LENGTH 2,
minute TYPE c LENGTH 2,
seconds TYPE c LENGTH 2,
END OF time.
FIELD-SYMBOLS <fs> TYPE any.
FINAL(t) = cl_demo_date_time=>get_user_time( ).
ASSIGN t TO <fs> CASTING TYPE time.
cl_demo_output=>display( <fs> ).
Example
In the following example, one of the two ASSIGN statements produces a runtime error because the alignment requirement for the type c is not met. Which of the statements produces the runtime error is not generally defined and depends on the preceding declarations.
DATA hex TYPE x LENGTH 10.
FIELD-SYMBOLS <fs> TYPE any.
ASSIGN hex+0(4) TO <fs> CASTING type c.
ASSIGN hex+1(4) TO <fs> CASTING type c.
... LIKE dobj
Description
The following can be specified after LIKE:
any, c, clike, csequence, data, and simple produce c with length 1.
decfloat produces decfloat34.
n produces n with length 1.
numeric and p produce p with length 8 and no decimal places.
x and xsequence produce x of the length 1.
Generic table types raise an exception of the class CX_SY_ASSIGN_CAST_ILLEGAL_CAST.
The field symbol <fs> can only be typed generically and not completely. The specified data type must
match the generic
typing of the field symbol, meaning that
castings are allowed to specialize the generic typing but not to make it more general.
Hints
Example
Casting of a structure with three components to a time field.
DATA:
BEGIN OF time,
hours TYPE c LENGTH 2 VALUE '11',
minute TYPE c LENGTH 2 VALUE '55',
seconds TYPE c LENGTH 2 VALUE '00',
END OF time.
FIELD-SYMBOLS <fs> TYPE any.
FINAL(t) = cl_demo_date_time=>get_user_time( ).
ASSIGN time TO <fs> CASTING LIKE t.
cl_demo_output=>display( <fs> ).
... [TYPE p] DECIMALS dec
Description
A numeric data object dec must be specified after DECIMALS. The assigned memory area is cast to the data type p, where the number of decimal places is determined by the content of dec. The number of decimal places must not exceed the total number of places. TYPE does not need to be specified for DECIMALS. If TYPE is used, only the data type p, which is used anyway, can be specified.
The field symbol <fs> can only be typed generically and not completely. The specified data type must
match the generic
typing of the field symbol, meaning that
castings are allowed to specialize the generic typing but not to make it more general.
Example
The calculation of the quotient from the packed number pack and the field symbol <pack> demonstrates the effect of casting with the addition DECIMALS. Factors between 10 and 100,000,000 are determined. When using <pack> in operand positions, a different value is used than when using pack.
DATA output TYPE TABLE OF string WITH EMPTY KEY.
DATA pack TYPE p LENGTH 8 DECIMALS 0 VALUE '12345678'.
FIELD-SYMBOLS <pack> TYPE p.
DO 8 TIMES.
ASSIGN pack TO <pack> CASTING DECIMALS sy-index.
APPEND CONV string( pack / <pack> ) TO output.
ENDDO.
cl_demo_output=>display( output ).