Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Assignments →  Setting References →  ASSIGN 

ASSIGN - casting_spec

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... { } 
  | { CASTING { { }
             | {TYPE type|(name)}
              | {LIKE dobj}
              | {[TYPE p] DECIMALS dec}
              | {TYPE HANDLE handle} } }
  | { obsolete_casting } ... .

Alternatives

1. ... { }
2. ... CASTING ...

Effect

The casting_spec specification determines with the data type that handles the memory area mem_area assigned to the field symbol if a statement contains the field symbol at an operand position. You can either make no specification or specify the addition CASTING. Outside of classes, the obsolete variants obsolete_casting are also possible.

Alternative 1

... { }

Effect

If you specify nothing for casting_spec, the field symbol takes on 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.

Alternative 2

... CASTING ...

Extras

1. ... { }
2. ... TYPE type|(name)

3. ... LIKE dobj

4. ... [TYPE p] DECIMALS dec

5. ... TYPE HANDLE handle

Effect

If you use the addition CASTING in casting_spec, the memory area is handled as if it had the type specified by CASTING. When specifying CASTING, the field symbol must not be typed with the obsolete STRUCTURE addition of the FIELD SYMBOLS statement.

Casting can either take place implicitly using the typing of the field symbol or explicitly using one of the additions TYPE, LIKE, or DECIMALS. For explicit casting, you must not specify the field symbol in full; instead specify it generically.


Notes

  • If the data type specified by CASTING is deep, the deep components have to appear in the assigned memory area in exactly the same way in terms of type and position. In particular, this means that individual reference variables can be assigned to only one field symbol that is typed as a reference variable by the same static type.
  • The static check of the ASSIGN statement using the CASTING addition is done is such a way that all errors are identified for Unicode and non- Unicode systems and for all platforms, regardless of the system and the platform on which the check is run. A runtime check only checks the data against the current system or the current platform. These checks likewise always take place if the field symbol <fs> is typed with the obsolete STRUCTURE addition of the FIELD SYMBOLS statement.
  • If you use the CASTING addition, the result of the ASSIGN statement may be platform-dependent. The internal byte order of characters in Unicode systems can for example be platform-dependent, which becomes obvious during a cast to a byte-like data type.
  • The memory area mem_area must meet the alignment requirements of the data type specified by the casting. When flat elementary data types are specified, for example, the memory address mem_area must be divisible as follows:

  • Divisible by 2 for the character-like data types c, n, d, and t (in Unicode systems).

  • By 4 for the numeric data type i.

  • By 8 for the numeric data types f and decfloat16.

  • By 16 for the numeric data type decfloat34.

Addition 1

... { }

Effect

If the CASTING addition 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 predefined ABAP types c, n, p, or x.

Addition 2

... TYPE type|(name)

Effect

After TYPE, you can specify either a data type type directly, or a character-like data object name in parentheses, which must contain the name of a data object in uppercase when the statement is executed. 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 predefined ABAP types c, n, p and x. However, you cannot specify table categories or 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 in Unicode programs.


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. 

Addition 3

... LIKE dobj

Effect

The following can be specified after LIKE:

  • A data object dobj based on the rules for TYPES ... LIKE. The assigned memory area is cast to the data type of the data object.
  • A generically typed field symbol.
  • If a memory area is assigned to the field symbol, the data type used to handle the memory area is the object of the cast.
  • If no memory area is assigned to the field symbol, a standard type produced by the following rules is used:

    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.
  • A generically typed formal parameter.
  • If an actual parameter is assigned to the formal parameter, the data type of this parameter is the object of the cast.

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

  • You can use LIKE to refer to the data objects in its own program, and also to the public attributes of global classes.
  • The standard type for generically typed field symbols specified after CASTING LIKE displays some differences to the standard type for generic field symbols and formal parameters (length 1 not 4 if any and data are used and no standard type for generic table types).
  • If a generically typed field symbol is specified after CASTING LIKE, a memory area should be assigned to it when the statement is executed.

Addition 4

... [TYPE p] DECIMALS dec

Effect

You must specify a numeric data object dec after DECIMALS. The assigned memory area is cast to the data type p, whereby the number of decimal places is determined by the content of dec. The number of decimal places must not exceed the number of decimal digits. You do not need to specify TYPE for DECIMALS. If you use TYPE, you can specify only the data type p (which is used anyway).

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 factor TYPE p LENGTH 8 DECIMALS 0. 
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. 
  factor = pack / <pack>. 
  WRITE / factor. 
ENDDO. 

Addition 5

... 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 object of the RTTS. The assigned memory area is cast to the type described by the type 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.


Note

The type object may have been created by using the RTTS methods on existing data objects, or by the dynamic definition of a new data type.

Continue

Casting Examples