3 weeks ago
Is there a way to pass sy-subrc as parameter in class constructor?
Even when passing by value (both defualt and explicitly with subrc=sy-subrc)
constructor IMPORTING VALUE(subrc) TYPE syst_subrc DEFAULT sy-subrc
inside constructor both subrc and sy-subrc are 0. When calling opther methods it works as expected.
3 weeks ago
When you call the constructor, are you using the sy-subrc from the calling programm directly? Or are you saving the value in a intermediate variable and use this intermediate variable when calling the constructor?
My guts tell me that the calling process sets the subrc in the calling programm and therefore changes the value that's been transfered.
3 weeks ago
Storing it in an intermediate variable works correctly. But that is additional line of code every time, wish I could just use sy-subrc directly.
Apparently just calling constructor sets it to 0 before even value is checked and copied to value parameter.
3 weeks ago
That's what I thought 😉
I am confused by your statement about the additional line every time. Usually you call a constructor only once to instanciate a object. Could you comment on why you say "every time"? Are you typing the call to the ctor multiple times? If so I wonder if there is a general flaw in your architecture. Or maybe I am misunderstanding your description.
3 weeks ago
Just that instead of doing
DATA(x) = NEW zcl_example( subrc = sy-subrc ).
or even skipping parameter if it has default value, I have to do
DATA(subrc) = sy-subrc.
DATA(x) = NEW zcl_example( subrc = subrc ).
I actually wanted to use it in exception, something like.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE zcx EXPORTING subrc = sy-subrc.
ENDIF.
3 weeks ago
Personally, I'd use the intermediate variable. sy-subrc's value gets changed so easily and so often. Even doing pass by value as you are, maybe sy-subrc's value is actually getting changed by the start of the call of the constructor even before the parameter is assigned. If you really need a stable sy-subrc value, then copy it to a local variable as you are doing. That's the only way to assure it stays at that particular value state.
a week ago - last edited a week ago
I've solved the problem by creating factory method, which correctly imports sy-subrc into helper variable, and then calls constructor.
CLASS-METHODS: throw IMPORTING VALUE(subrc) TYPE syst_subrc DEFAULT SY-SUBRC RAISING zcx.
...
METHOD throw.
RAISE EXCEPTION TYPE zcx EXPORTING subrc = subrc.
ENDMETHOD.
...
IF sy-subrc <> 0.
zcx=>throw( ).
ENDIF.
ble.