Skip to content

ABAP Keyword Documentation →  ABAP Programming Guidelines →  Robust ABAP →  Assignments, Calculations, and Other Types of Data Access 

Using Conversion Rules

Other versions: 7.31 | 7.40 | 7.54

Background

ABAP contains numerous conversion rules for assignments between data objects of different data types. These rules relate to assignments between:

  • Elementary data objects
  • Elementary data objects and structures
  • Structures
  • Internal tables
  • Reference Variables

Only 2 of the 144 possible assignments (between data objects of the 12 different elementary data types) are not allowed: from d to t and the other way round. All other assignments are allowed — and almost every assignment has its own conversion rules. It is especially important to have rules for assignments between data objects of the same data type, if different technical properties (such as length or number of decimal places) are allowed. The EXACT addition for the MOVE statement only allows conversions that produce valid values and where no values are lost.

Rule

Avoid unexpected conversion results

Only assign data objects to each other if the content corresponds to the data type of the target field and produces an expected result. Do not exploit every ABAP conversion rule to its full extent.

Details

The ABAP conversion rules are based on the philosophy that assignments should be allowed between as many combinations of values as possible, without generating exceptions. In this situation, ABAP behaves quite differently from other programming languages. In other languages, assignments between different data types are usually handled much more strictly and special conversion routines or explicit casting for specific requested conversions are used.

Although it is convenient to be able to readily assign all possible data objects to each other, there are also disadvantages, such as the generation of invalid values. Another example is implicit casting, which occurs when assignments are made between elementary data objects and structures, or between incompatible structures.

Even if no invalid values are generated, problems still can occur. If valid target values are generated from invalid source values, this does not necessarily meet the expectations of the reader and it can make program maintenance considerably difficult. One example of this is the handling of invalid content in the source field in assignments from a character-like type to a byte-like type. Instead of exiting the assignment with an exception, hexadecimal zeros are passed from the first invalid character.

The only solution here is the EXACT addition for the MOVE statement for lossless assignments, which raises an exception in these cases. Even though this is a bit late in the day, the behavior of an assignment with the EXACT addition could be regarded as the normal, expected behavior. Other unexpected behaviors represent an implementation of special rules, which is actually the standard behavior in ABAP.

Bad example

Anyone who is familiar with all the details of the ABAP conversion rules would probably expect an exception when the text in the following source code is assigned to the numeric text. However, only the digits of the text are respected. Therefore, the target field is given the value "00000043" instead of the value "00000007", which might also be expected.

DATA: text TYPE string,
num_text TYPE n LENGTH 8.
...
text = '4 Apples + 3 Oranges'.
...
num_text = text.

Good example

This issue is corrected in the source code below. The EXACT addition for the MOVE statement is used, which triggers an exception.

...
text = '4 Apples + 3 Oranges'.
...
TRY.
     MOVE EXACT text TO num_text.
  CATCH cx_sy_conversion_error.
     ...
ENDTRY.