SAP CONSTRUCTOR EXPRESSION CONV

Get Example source ABAP code based on a different SAP table
  


ARTICLE
• CONV ABAP_OPERATOR
• # CONV

CONV - Conversion Operator

Syntax
... CONV type( dobj ) ...

Effect
A constructor expression with the conversion operator CONV converts the argument dobj to the data type specified using type and creates an appropriate result. The following can be specified for type:
A non-generic data type dtype (with the exception of reference types).

ABAP Code Snippet
The parentheses must contain precisely one unnamed argument dobj that can be converted to the data type type. dobj is a general expression position with the following restrictions:
If dobj is specified as a string expression, type must be character-like.
If dobj is specified as a bit expression, type must be byte-like.
The content of the result is determined by an assignment of the argument in accordance with the associated conversion rule. If dobj is compatible with the data type type, CONV does not need to be used and a syntax check warning is produced.

Notes
The conversion operator CONV is suitable for avoiding the declaration of helper variables only needed to (for example)
specify type-compliant actual parameters.
influence the calculation type of an arithmetic expression or the comparison type of a relational expression.
The argument of CONV can itself be a calculation expression, which means that CONV can be used within a calculation expression to transform results of subcalculations to a specific type.
No empty parentheses can be specified after CONV to construct an initial value of the specified type. The expression VALUE #( ) can be used to do this.
For reference types, the conversion operator CONV is not necessary, since these involve only castings and no conversions. The operator CAST is used for castings.

Example
The method CONVERT_TO of the class CL_ABAP_CODEPAGE expects the data type string for the input parameter SOURCE. CONV is used to convert a text field to this data type, directly in the operand position. DATA file TYPE xstring.
DATA text TYPE c LENGTH 255.

file = cl_abap_codepage=>convert_to(
source = CONV string( text )
codepage = `UTF-8` ).

Example
Even though the internal table itab in the method meth1 has the same row type as the table type of the parameter para of the method meth2, it cannot be passed directly due to its different table category and key. CONV is used to convert itab to the required table type. CLASS class DEFINITION.
PUBLIC SECTION.
TYPES t_itab TYPE STANDARD TABLE OF i
WITH EMPTY KEY.
METHODS meth1.
PRIVATE SECTION.
METHODS meth2 IMPORTING para TYPE t_itab.
ENDCLASS.

CLASS class IMPLEMENTATION.
METHOD meth1.
DATA itab TYPE STANDARD TABLE OF i
WITH NON-UNIQUE DEFAULT KEY.
...
meth2( CONV #( itab ) ).
...
ENDMETHOD.
METHOD meth2.
...
ENDMETHOD.
ENDCLASS.

Example
The two calculations produce different results. In the first case, the calculation type is f and the end result is converted to i. In the second case, CONV converts each interim result to the calculation type i. DATA int TYPE i.

int = sqrt( 5 ) + sqrt( 6 ).
int = CONV i( sqrt( 5 ) ) + CONV i( sqrt( 6 ) ).

Example
The first logical expression is false, as specified in the comparison rules for character-like data types . CONV is used to alter the comparison type of the second comparison so that the comparison is true. TYPES c1 TYPE c LENGTH 1.

DATA txt TYPE c1.
DATA str TYPE string.

txt = ' ' .
str = ` `.

IF txt = str.
...
ENDIF.

IF txt = CONV c1( str ).
...
ENDIF.