Advertising:
Cond(ABAP keyword): Difference between revisions
From SAP Knowledge Base
(Created page with "category:Code_Snippets COND can be used since ABAP 7.40. This is an alternative to IF, especially because the code is much shorter, but also sometimes more confusing.<br /> The advantage is to set a condition directly when assigning a value to a variable or to parameters in methods. === Mit COND === Here the COND is used in a method call to pass a parameter like a subobject for the application log: <syntaxhighlight lang="abap" line start="1"> bal = cf_reca_message_l...") |
|||
Line 19: | Line 19: | ||
Without COND, you would have to use IFs to query which mode is currently active in order to assign the correct sub-object: | Without COND, you would have to use IFs to query which mode is currently active in order to assign the correct sub-object: | ||
<syntaxhighlight lang="abap" line start="1"> | <syntaxhighlight lang="abap" line start="1"> | ||
DATA: subobject TYPE balsubobj. | DATA: subobject TYPE balsubobj. | ||
Revision as of 17:16, 21 December 2024
COND can be used since ABAP 7.40. This is an alternative to IF, especially because the code is much shorter, but also sometimes more confusing.
The advantage is to set a condition directly when assigning a value to a variable or to parameters in methods.
Mit COND
Here the COND is used in a method call to pass a parameter like a subobject for the application log:
bal = cf_reca_message_list=>create( EXPORTING id_object = 'YOBJECT'
id_subobject = COND balsubobj( WHEN mode = 'a' THEN 'YOBJECTSUBA'
WHEN mode = 'b' THEN 'YOBJECTSUBB'
WHEN mode = 'c' THEN 'YOBJECTSUBC'
ELSE THROW cx_root "ELSE is optional
)
).
After COND, the data type can be seen. Here you could also have worked with #, since the type is already known to id_suboject. If you were to make an inline declaration where the type is not known at all, you would have to specify it after COND.
With IFs
Without COND, you would have to use IFs to query which mode is currently active in order to assign the correct sub-object:
DATA: subobject TYPE balsubobj.
IF mode = 'a'.
subobject = 'YOBJECTSUBA'
ELSEIFmode = 'b'.
subobject = 'YOBJECTSUBB'
ELSEIF mode = 'c'.
subobject = 'YOBJECTSUBC'
ELSE
RAISE EXCEPTION TYPE cx_root.
"other error handling...
ENDIF.
bal = cf_reca_message_list=>create( EXPORTING id_object = 'YOBJECT'
d_subobject = subobject ).