Show TOC

Casting Data ObjectsLocate this document in the navigation structure

When you assign a data object to a field symbol, you can cast to any data type. This means that any area of memory can be viewed as having assumed a given type. You can then address parts of fields symbolically without having to use offset/length addressing.

A cast is performed using the CASTINGaddition of the ASSIGN statement. The CASTINGaddition allows you to assign a data object to a field symbol where the type of the data object is incompatible with that of the field symbol. There are two types of casting: casting with an implicit type declaration and casting with an explicit type declaration.

The CASTING addition replaces some obsolete additions of the ASSIGN statement that should no longer be used.

Casting with an Implicit Type Declaration

Provided the field symbol is either fully typed or has one of the generic built-in ABAP types - c, n, p, or x - you can use the following ASSIGNstatement:

ASSIGN ...TO <fs> CASTING.

When the system accesses the field symbol, the content of the assigned data object is interpreted as if it had the same type as the field symbol. The length and alignment of the data object must be compatible with the field symbol type. Otherwise the system returns a runtime error. If the type of either the field symbol or the data object is - or contains - a string, reference type, or internal table, the type and position of these components must match exactly. Otherwise, a runtime error occurs.

Tip

REPORT demo_field_symbols_casting.

TYPES: BEGIN OF t_date,          year(4)  TYPE n,          month(2) TYPE n,          day(2)   TYPE n,       END OF t_date.

FIELD-SYMBOLS <fs> TYPE t_date.

ASSIGN sy-datum TO <fs> CASTING.

WRITE / sy-datum.SKIP.WRITE: / <fs>-year , / <fs>-month, / <fs>-day.

The output looks something like this:

1999/05/19

19990519

In this example, the field symbol <fs> is fully typed using the local program type t_date. The sy-datum field can be treated like a structure as a result of the CASTINGaddition of the ASSIGN statement. This would not be possible without the CASTING addition, since sy-datum is incompatible with the field symbol type.

Casting with an Explicit Type Declaration

If the field symbol is neither fully typed nor generically typed, use the following form of the ASSIGNstatement:

ASSIGN ...TO <fs> CASTING {TYPE type}|{LIKE dobj} [DECIMALS dec].

When the system accesses the field symbol, the content of the assigned data object is interpreted as if it had the type declared in the statement. After the TYPE addition, you can declare the name of a data object enclosed in parentheses. The content of this data object indicates the data type at runtime.

You cannot enter a type unless an explicit cast to the data object is logical. For example, there is no point casting a standard table to a sorted table, or an object reference to a data reference. Thus, you cannot declare table types (such as SORTED TABLE) or reference types (using REF TO) after the TYPEaddition. If the data type contains implicit string, reference types, or internal tables, these components must also occur in the assigned data object with their type and position.. Otherwise, the system returns a syntax or runtime error.

The data type declared in the TYPE or LIKEaddition must be compatible with the generic type of the field symbol. The generic type can be left as it is or specialized. However, you cannot reset known technical attributes of the field symbol. For example, if the field symbol has the completely generic type ANY , you can declare another permitted generic type. If the field symbol has the generic type c, n, p, or x, you can only specify the length and the number of decimal places. You cannot make an explicit type declaration for a fully typed field symbol, since, if the field symbol is fully typed already, you cannot specialize it further. In any case, the system checks statically for this at runtime, where possible.

You should use the DECIMALS addition if the content of the assigned data object can be interpreted as a packed number. The field symbol is then given dec decimal places. DECIMALS can only be used if no type specification is made of if the type p is specified after TYPE. If there is no type specification, type p is used implicitly. Moreover, you cannot use the LIKE and DECIMALS additions together.

Tip

REPORT demo_field_symbols_casting_typ.

TYPES: BEGIN OF t_date,          year(4)  TYPE n,          month(2) TYPE n,          day(2)   TYPE n,       END OF t_date.

FIELD-SYMBOLS: <fs> TYPE ANY,               <f>  TYPE n.

ASSIGN sy-datum TO <fs> CASTING TYPE t_date.

WRITE / sy-datum.

SKIP.

DO.  ASSIGN COMPONENT sy-index OF STRUCTURE <fs> TO <f>.  IF sy-subrc <> 0.    EXIT.  ENDIF.  WRITE / <f>.ENDDO.

The output looks something like this:

1999/05/19

19990519

In this example, the field symbol <fs> is completely generic. A cast is performed on the field sy-datum  to the local program type t_date using the CASTING addition of the ASSIGN statement. The field symbol <fs> can now be treated like a structure, but it does not know any components. Therefore, it must be assigned - component by component - to another field symbol <f>.