Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Can you pass sy-subrc as parameter in class constructor?

kaszub09
Explorer
0 Kudos
301

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.

6 REPLIES 6

radinator
Participant
0 Kudos
234

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.

0 Kudos
227

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.

radinator
Participant
0 Kudos
220

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.

0 Kudos
205

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.

 

thomas_jung
Developer Advocate
Developer Advocate
177

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.

 

kaszub09
Explorer
0 Kudos
94

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.