SAP CASTING - Guide
Get Example source ABAP code based on a different SAP table
Casting
ABAP_BACKGROUND
Casting refers to the process of handling a data object by assuming a certain data type. This definition is different to the meaning of the concept in other programming languages, such as Java. Here, casting means a different concept which is referred to as 'conversion' in ABAP. Casting in ABAP is either explicit or implicit:
ABAP_RULE
Avoid implicit casting. If a cast to another data type is required, it can usually be implemented explicitly using
ABAP_DETAILS
Implicit casting can potentially occur if structures are used as follows:
The use of implicit casting is prone to errors and produces source code that is difficult to understand. If the
ABAP_EXAMPLE_BAD
The following source code shows the assignment of a text string to a structure with only character-like components. An implicit casting occurs here, which is regarded as unwanted according to the above rule. The entire structure is handled as a text field of type
comp1 TYPE c LENGTH 2,
comp2 TYPE c LENGTH 4,
END OF structure.
DATA structure TYPE structure.
DATA text TYPE string.
...
text = ...
structure = text.>
ABAP_EXAMPLE_END
ABAP_EXAMPLE_GOOD
The following source code improves the example above, by assigning the structure to a field symbol of type c. Explicit casting occurs. Only the character-like field symbol is used to handle the structure as a character-like field.
FIELD-SYMBOLS < text> TYPE c.
...
ASSIGN structure TO < text> CASTING.
< text> = ...>
ABAP_EXAMPLE_END