ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Assignments →  Assigning References →  Setting Field Symbols →  ASSIGN →  ASSIGN - casting_spec →  Casting Examples → 

Field Symbols, Casting Predefined Data Types

This example demonstrates how a casting is performed on predefined data types.

Source Code

REPORT demo_field_symbols_assign_type.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.

    DATA txt(8) TYPE c VALUE '20050606'.

    DATA mytype(1) VALUE 'X'.

    FIELD-SYMBOLS <fs> TYPE ANY.

    DATA(out) = cl_demo_output=>new(
      )->begin_section( 'Cast with built-in types' ).

    ASSIGN txt TO <fs>.
    out->write( |<fs> with inherited type c: { <fs> }| ).

* correct -------------------------------------------------------------

    ASSIGN txt TO <fs> CASTING TYPE i.
    out->write( |<fs> casted with i: { <fs> }| ).

    ASSIGN txt TO <fs> CASTING TYPE (mytype).
    out->write( |<fs> casted with x: { <fs> }| ).

    out->display( ).

* obsolete, not allowed in methods ------------------------------------

    "ASSIGN txt TO <fs> TYPE 'D'.

    "ASSIGN txt TO <fs> TYPE mytype.

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

This example demonstrates how casting works on predefined data types. First the character string txt is assigned to the field symbol <fs> without casting. Afterwards, txt is assigned to <fs> using casting on types i and x. The value of the second task depends on the byte sequence of the current platform. The paired numbers in the last output row represent the hexadecimal code for the character in txt and depend on the character display of the current application server.

The section of the method that has been commented out also shows the syntax for the relevant obsolete casting.