01-26-2009 6:56 PM
Hi,
I am trying to learn OOPS. I have tried to develop a CLASS just to add & subtract 2 variables (which will be given as input parameters).. Plz find my code below..
When I try to check syntax for the code, it gives me error "The obligatory parameter 'OP1' had no value assigned to it"... Can anyone plz help..
I might have missed some basic step..
REPORT ZVG_OOPS.
class amount definition deferred.
PARAMETERS: p_op1 LIKE bseg-dmbtr OBLIGATORY,
p_op2 LIKE bseg-dmbtr OBLIGATORY.
DATA: addnsub TYPE REF TO amount.
CLASS amount DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING op1 TYPE dmbtr
op2 TYPE dmbtr,
add IMPORTING op1 TYPE dmbtr
op2 TYPE dmbtr
EXPORTING result TYPE dmbtr,
subtract IMPORTING op1 TYPE dmbtr
op2 TYPE dmbtr
EXPORTING result TYPE dmbtr.
PRIVATE SECTION.
DATA: result TYPE dmbtr.
ENDCLASS.
CLASS amount IMPLEMENTATION.
METHOD constructor.
IF op1 < op2.
MESSAGE e029(ZR) with text-001 p_op1 p_op2.
ENDIF.
ENDMETHOD.
METHOD add.
me->result = op1 + op2.
ENDMETHOD.
METHOD subtract.
me->result = op1 - op2.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: r_op TYPE dmbtr.
CREATE OBJECT addnsub.
addnsub-> constructor( EXPORTING op1 = p_op1
op2 = p_op2).
addnsub->add( EXPORTING op1 = p_op1
op2 = p_op2
IMPORTING result = r_op ).
Thanks
Geetha
01-26-2009 6:59 PM
CONSTRUCTOR would be called automatically when you instantiate the object using the CREATE OBJECT.
So, you should actually do like this:
*CREATE OBJECT addnsub.
*
*addnsub-> constructor( EXPORTING op1 = p_op1
*op2 = p_op2).
CREATE OBJECT addnsub
EXPORTING
op1 = p_op1
op2 = p_op2.
Regards,
Naimesh Patel
01-26-2009 6:59 PM
CONSTRUCTOR would be called automatically when you instantiate the object using the CREATE OBJECT.
So, you should actually do like this:
*CREATE OBJECT addnsub.
*
*addnsub-> constructor( EXPORTING op1 = p_op1
*op2 = p_op2).
CREATE OBJECT addnsub
EXPORTING
op1 = p_op1
op2 = p_op2.
Regards,
Naimesh Patel
01-26-2009 7:12 PM
Thanks Naimesh.. That answers me!!!! Thanks again for your quick response..
01-26-2009 7:18 PM
Hi
Another question..
Now that my code is free of errors, when I execute my code,
REPORT ZVG_OOPS.
class amount definition deferred.
PARAMETERS: p_op1 LIKE bseg-dmbtr OBLIGATORY,
p_op2 LIKE bseg-dmbtr OBLIGATORY.
DATA: addnsub TYPE REF TO amount.
CLASS amount DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING op1 TYPE dmbtr
op2 TYPE dmbtr,
add IMPORTING op1 TYPE dmbtr
op2 TYPE dmbtr
EXPORTING result TYPE dmbtr,
subtract IMPORTING op1 TYPE dmbtr
op2 TYPE dmbtr
EXPORTING result TYPE dmbtr.
PRIVATE SECTION.
DATA: result TYPE dmbtr.
ENDCLASS.
CLASS amount IMPLEMENTATION.
METHOD constructor.
IF op1 < op2.
MESSAGE e029(ZR) with text-001 p_op1 p_op2.
ENDIF.
ENDMETHOD.
METHOD add.
me->result = op1 + op2.
ENDMETHOD.
METHOD subtract.
me->result = op1 - op2.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: r_op TYPE dmbtr.
CREATE OBJECT addnsub EXPORTING op1 = p_op1
op2 = p_op2 .
addnsub->add( EXPORTING op1 = p_op1
op2 = p_op2
IMPORTING result = r_op ).
WRITE:/ r_op.
When the method ADD is triggered... me->result gets the added value but its not passed to r_op.. Can you plz help..
Thanks
Geetha
01-26-2009 7:36 PM
In the add-method, you are adding OP1 and OP2 and put this value in the private attribute RESULT.
So when you want to retrieve the result you should say something like this:
result (which is the export parameter from the method, and NOT the private attribute) = op1 + op2.
In the current code the result is stored in the private attribute RESULT.
METHOD add.
me->result = op1 + op2.
ENDMETHOD.
me->result => PRIVATE SECTION. DATA: result TYPE dmbtr.
01-26-2009 8:05 PM
Not very clear.. can you plz clarify...
How my code should be?
I tried to declare RESULT in PUBLIC SECTION...
Still, I did not get the value in r_op..
01-26-2009 8:43 PM
Since your expecting the result back in the RESULT parameter, you need to assign the result in that value.
METHOD add.
me->result = op1 + op2.
result = me->result.
ENDMETHOD.
METHOD subtract.
me->result = op1 - op2.
result = me->result.
ENDMETHOD.
If you don't want to use the attribute RESULT in your any calculation than you don't need it. You can just pass the two parameters and get back the result in the exporting parameter.
Regards,
Naimesh Patel
01-26-2009 10:09 PM
01-26-2009 7:18 PM