Advertising:
Cond(ABAP keyword): Difference between revisions
From SAP Knowledge Base
No edit summary |
|||
Line 1: | Line 1: | ||
[[category: | [[category:keywords]] | ||
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 /> | 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. | The advantage is to set a condition directly when assigning a value to a variable or to parameters in methods. |
Revision as of 23:11, 25 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 ).