Entering content frameCasting Data Objects Locate the document in its SAP Library 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 considered to have a assumed a given type. You can then address parts of fields symbolically without having to use offset/length addressing.

Use the CASTING addition to the ASSIGN statement. This 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.

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 statement:

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.

Example

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

1999
05
19

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 CASTING addition to 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 ASSIGN statement:

ASSIGN ... TO <FS> CASTING TYPE <type>|LIKE <obj> [DECIMALS <d>].

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 kinds (such as SORTED TABLE) or reference types (using REF TO) after the TYPE addition. If the data type is – or contains – a string, reference type, or internal table, these components must occur in the assigned data object. Otherwise, the system returns a syntax or runtime error.

The data type declared in the TYPE or LIKE addition 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. The system checks this statically if possible, or at runtime if not.

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 <d> decimal places. DECIMALS may only be used if no type was defined or if type P is defined. If no type is defined, type P is used. Moreover, you cannot use the LIKE and DECIMALS additions together.

Example

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

1999
05
19

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 to the ASSIGN statement. Field symbol <fs> can now be handled like a structure, but does not recognize any components. It therefore must be assigned to another field symbol <f> component by component.

Obsolete Variants of the ASSIGN Statement

The following additions to the ASSIGN statement are obsolete; you should no longer use them. They have been replaced by the above CASTING addition.

Cast with built-in data types

To set the data type of a field symbol independently of the assigned object type (before the CASTING statement was introduced), you used the TYPE addition in the ASSIGN statement:

ASSIGN ... TO <FS> TYPE <t>.

However, you could only use the fixed-length elementary ABAP types (C, D, F, I, N, P, T, and X), ‘s’ for two-byte integers (with sign) and ‘b’ for one byte integers (without sign) as literals or variables for <t>. By contrast, the CASTING addition allows you declare any data type after the TYPE addition.

There were situations where you could have used a TYPE addition:

The TYPE addition with a typed field symbol was useful whenever the assigned data object type is incompatible with the field symbol type, but you want to avoid triggering an error message. In this case, the specified type <t> must be compatible with the field symbol type. The field symbol then retains its data type, regardless of the assigned data object. You should now use casting with an implicit type declaration in this situation.

If you use the TYPE addition, an untyped field symbol <FS> adopts the data type specified in <t> instead of the data type and output length of the data object assigned to it. If the TYPE addition is included in the ASSIGN statement and the field symbol is then used in the program, the content of the data object is interpreted as if it were a type <t> field. You should now use casting with an explicit type declaration in this situation.

An error message occurs: if the data type declared is unknown; if the length of the data type declared is too short for the assigned field; or if the alignments of the data type and field symbol are incompatible.

Example

Example using the obsolete TYPE addition:

DATA TXT(8) VALUE '19980606'.

DATA mytype(1) VALUE 'X'.

FIELD-SYMBOLS <fs>.

ASSIGN txt TO <fs>.
WRITE / <fs>.

ASSIGN txt TO <fs> TYPE 'D'.
WRITE / <fs>.

ASSIGN TXT TO <FS> TYPE MYTYPE.
WRITE / <fs>.

Example using the CASTING addition:

DATA TXT(8) VALUE '19980606'.

DATA mytype(1) VALUE 'X'.

FIELD-SYMBOLS <fs>.

ASSIGN txt TO <fs>.
WRITE / <fs>.

ASSIGN txt TO <fs> CASTING TYPE d.
WRITE / <fs>.

ASSIGN txt TO <fs> CASTING TYPE (mytype).
WRITE / <fs>.

In both cases, the system displays the following:

19980606

06061998

3139393830363036

In these examples, string TXT is assigned to field symbol <FS> three times: first without casting, then casting with type D and finally with casting with type X. The format of the second output line depends on the data in the user master record. The numbers in the last line are the hexadecimal codes of the characters in txt . They are platform-specific (in this case, ASCII).

Casting decimal places

To specify the number of decimal places in a field symbol when assigning it to a packed number of type P (before the CASTING statement was introduced), you used the DECIMAL addition in the ASSIGN statement:

Syntax

ASSIGN ... TO <FS> DECIMALS <d>.

You can use the DECIMALS addition in any variant of the ASSIGN statement where the data object assigned to the field symbol has type P. In general, this leads to different numerical values for the field symbol and the assigned field. You can specify the number of decimal places <d> as a literal or a variable. If <d> is not between 0 and 14, or the data object assigned to the field symbol is not a type P field, a runtime error occurs.

Example

Example using the obsolete DECIMALS addition:

DATA: pack1 TYPE p DECIMALS 2 VALUE '400',
      pack2 TYPE p DECIMALS 2,
      pack3 TYPE p DECIMALS 2.

FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE i.

WRITE: / 'PACK1', pack1.

ASSIGN pack1 TO <f1> DECIMALS 1.
WRITE: / '<F1> ', <f1>.

pack2 = <f1>.
WRITE: / 'PACK2', pack2.

ASSIGN pack2 TO <f2> DECIMALS 4.
WRITE: / '<F2> ', <f2>.

pack3 = <f1> + <f2>.
WRITE: / 'PACK3', pack3.

<f2> = '1234.56789'.
WRITE: / '<F2> ', <f2>.
WRITE: / 'PACK2', pack2.

Example using the CASTING addition:

DATA: pack1 TYPE p DECIMALS 2 VALUE '400',
      pack2 TYPE p DECIMALS 2,
      pack3 TYPE p DECIMALS 2.

FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE i.

WRITE: / 'PACK1', pack1.

ASSIGN pack1 TO <f1> CASTING TYPE p DECIMALS 1.
WRITE: / '<F1> ', <f1>.

pack2 = <f1>.
WRITE: / 'PACK2', pack2.

ASSIGN pack2 TO <f2> CASTING TYPE p DECIMALS 4.
WRITE: / '<F2> ', <f2>.

pack3 = <f1> + <f2>.
WRITE: / 'PACK3', pack3.

<f2> = '1234.56789'.
WRITE: / '<F2> ', <f2>.
WRITE: / 'PACK2', pack2.

In both cases, the list appears as follows:

PACK1 400.00

<F1> 4,000.0

PACK2 4,000,00

<F2> 40.0000

PACK3 4,040.00

<F2> 1.234,5679

PACK2 123,456.79

Each of the three type P fields has two decimal places. The field symbols <f1> and <f2> have one and four decimal places each. Note that the numeric values are different for the field symbols and for their assigned fields.

 

 

Leaving content frame