SAP NetWeaver AS ABAP Release 752, ©Copyright 2017 SAP AG. All rights reserved.
ABAP - Keyword Documentation → ABAP - Reference → Processing Internal Data → Assignments → Assigning References → Setting Field Symbols → ASSIGN →
ASSIGN - casting_spec
Syntax
... { }
| { CASTING { { }
| {TYPE type|(name)}
| {LIKE dobj}
| {[TYPE p] DECIMALS dec}
| {TYPE HANDLE handle} } }
| { obsolete_casting } ...
Alternatives:
Effect
If specified, casting_spec defines the data type used to handled the memory area mem_area assigned to the field symbol when a statement is given the field symbol in an operand position. Either the addition CASTING can be specified or nothing at all. Outside of classes, the obsolete variants obsolete_casting are also possible.
The following restrictions apply:
... { }
Effect
If nothing is specified for casting_spec, the field symbol is given 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. Statement DESCRIBE FIELD returns the corresponding values C 3 and g.
FIELD-SYMBOLS <fs> TYPE csequence.
ASSIGN 'xxx' TO <fs> .
DESCRIBE FIELD <fs> TYPE DATA(type)
LENGTH DATA(length) IN CHARACTER MODE.
cl_demo_output=>write( type ).
ASSIGN `xxx` TO <fs> .
DESCRIBE FIELD <fs> TYPE type.
cl_demo_output=>display( type ).
... CASTING ...
Extras:
1. ... { }
2. ... TYPE type|(name)
3. ... LIKE dobj
4. ... [TYPE p] DECIMALS dec
5. ... TYPE HANDLE handle
Effect
If the addition CASTING is used in casting_spec, the memory area is handled as if it had the type specified by CASTING. If CASTING is specified, the field symbol must not be typed with the obsolete addition STRUCTURE of the statement FIELD SYMBOLS. .
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 cannot be specified in full; instead it must be specified generically.
Notes
... { }
Effect
If the addition CASTING is specified without any further additions, the assigned
memory area is cast to the type of the field symbol. The field symbol must be either fully typed, or
typed with one of the generic built-in ABAP types c, n, p, or x.
Example
Casting of an 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)
Effect
Specifies a data types after TYPE explicitly. The name of the type can be specified as follows:
The assigned memory area is cast to the specified type. The data type specified after TYPE cannot be generic. The exceptions to this rule are the built-in ABAP types c, n, p, and x. However, table categories cannot be specified, nor REF TO.
The field symbol <fs> can only be typed generically and not in full. The specified data type has to match the generic typing of the field symbol, meaning that castings are allowed to specialize the generic typing but not to make 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.
ASSIGN sy-timlo 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, since 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
Effect
The following can be specified after LIKE:
The field symbol <fs> can only be typed generically and not in full. The specified data type has to
match the generic
typing of the field symbol, meaning that
castings are allowed to specialize the generic typing but not to make more general.
Notes
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.
ASSIGN time TO <fs> CASTING LIKE sy-timlo.
cl_demo_output=>display( <fs> ).
... [TYPE p] DECIMALS dec
Effect
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 in full. The specified data type has to
match the generic
typing of the field symbol, meaning that
castings are allowed to specialize the generic typing but not to make more general.
Example
Calculating 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 ).
... TYPE HANDLE handle
Effect
After TYPE HANDLE, a reference variable handle of the static type of the CL_ABAP_DATADESCR class or its subclasses is specified and it points to a type description object of the RTTS. The assigned memory area is cast to the type described by the type description object.
The field symbol <fs> can only be typed generically and not in full. The specified data type has to match the generic typing of the field symbol, meaning that castings are allowed to specialize the generic typing but not to make more general.
The addition CASTING TYPE HANDLE cannot be used with the addition RANGE.
Note
The type description object may have been created using the RTTS methods on existing data objects, or using the definition of a new data type.
Example
Create a type description object for a structure and use it to cast a date field.
DATA(struct) = cl_abap_structdescr=>create(
VALUE abap_component_tab(
( name = 'YEAR' type = cl_abap_elemdescr=>get_c( p_length = 4 ) )
( name = 'MONTH' type = cl_abap_elemdescr=>get_c( p_length = 2 ) )
( name = 'DAY' type = cl_abap_elemdescr=>get_c( p_length = 2 ) )
) ).
FIELD-SYMBOLS <fs> TYPE any.
ASSIGN sy-datlo TO <fs> CASTING TYPE HANDLE struct.
cl_demo_output=>display( <fs> ).