‎2007 Mar 15 11:58 PM
Trying out few oop codes....
While calling class instance methods and passing parameters, when to use the following syntax.
data: cvar type ref to class1.
cvar->method( exporting variable1 = value )
(or) some time i see sample codes with out key word 'exporting'
cvar->method( variable1 = value ) .
(or)
cvar->method( value ) .
(or) some times with key word CALL METHOD
CREATE OBJECT cvar
CALL METHOD cvar->method
EXPORTING
variable1 = value.
Tried out a uniform way of calling ,but getting errors.Any inputs please..
Thanks,
Bvan
‎2007 Mar 16 12:06 AM
Well, in newer releases, you can omit the CALL METHOD, this is not true in 46c except for when you are having a RETURN parameter.
Also, if there is only one IMPORTing parameter you may use this syntax.
cvar->method( value ) .
or
cvar->method( variable1 = value ) .
but if there is more than one IMPORTING parameter or there are also EXPORTING or CHANGING parameters you should use this.
cvar->method( exporting variable1 = value
variable 2 = value )
Yes, I know, it is a little confusing.
Regards,
RIch Heilman
‎2007 Mar 16 2:52 AM
Bhavan,
First declare the class.
Implement the class.
Declare the Class reference variable
Create the class object.
call the method.
data: cvar type ref to class1.
CREATE OBJECT cvar
Calling Methods
To call a method, use the following statement:
CALL METHOD <meth> EXPORTING... <ii> =.<f i>...
IMPORTING... <ei> =.<g i>...
CHANGING ... <ci> =.<f i>...
RECEIVING r = h
EXCEPTIONS... <ei> = rc i...
The way in which you address the method <method> depends on the method itself and from where you are calling it. Within the implementation part of a class, you can call the methods of the same class directly using their name <meth>.
CALL METHOD <meth>...
Outside the class, the visibility of the method depends on whether you can call it at all. Visible instance methods can be called from outside the class using
CALL METHOD <ref>-><meth>...
where <ref> is a reference variable whose value points to an instance of the class. Visible instance methods can be called from outside the class using
CALL METHOD <class>=><meth>...
where <class> is the name of the relevant class.
When you call a method, you must pass all non-optional input parameters using the EXPORTING or CHANGING addition in the CALL METHOD statement. You can (but do not have to) import the output parameters into your program using the IMPORTING or RECEIVING addition. Equally, you can (but do not have to) handle any exceptions triggered by the exceptions using the EXCEPTIONS addition. However, this is recommended.
You pass and receive values to and from methods in the same way as with function modules, that is, with the syntax:
... <Formal parameter> = <Actual parameter>
after the corresponding addition. The interface parameters (formal parameters) are always on the left-hand side of the equals sign. The actual parameters are always on the right. The equals sign is not an assignment operator in this context; it merely serves to assign program variables to the interface parameters of the method.
If the interface of a method consists only of a single IMPORTING parameter, you can use the following shortened form of the method call:
CALL METHOD <method>( f).
The actual parameter <f> is passed to the input parameters of the method.
If the interface of a method consists only of IMPORTING parameters, you can use the following shortened form of the method call:
CALL METHOD <method>(....<ii> =.<f i>...).
Each actual parameter <f i > is passed to the corresponding formal parameter <i i >.
Pls. mark if useful
‎2007 Mar 16 8:14 AM
If there is only one importing parameter use this format.
cvar->method( value ) .
or
cvar->method( variable1 = value ) .
but if there is more than one importing parameter together with exporting or changing parameters then uset his format.
cvar->method( exporting variable1 = value
variable 2 = value )
The best way to aviod such conflicts is by using the pattern command-> Abap objects-> and then specify your parameters....This is the safest way as the system inserts the signature for the method
Hope this helps.....Please reward points if useful...
‎2007 Mar 16 3:01 PM
Hello Bhavan,
There are 2 ways of method calls in ABAP.
1. CALL METHOD <ref_name>-><method_name>
EXPORTING par_im = ex_val
IMPORTING par_ex = im_val
..
2. There is another equivalent of this which uses a shorter syntax - in which
CALL METHOD prefix is not used.
<ref_name>-><method_name> ( EXPORTING par_im = ex_val
IMPORTING par_ex = im_val
.. ).
Unlike the first method the parameters are listed in brackets in this way.
Now when we call a method that has only 1 import parameter , that actual
parameter can be specified in brackets without any other additions. When we
call a method that has only import parameters, EXPORTING addition can be
omitted.
E.g. <ref_name>-><method_name> ( par_im = ex_val ).
Regards
Indrajit.
‎2007 Mar 16 4:14 PM
hi,
there are different ways in calling a method,check the simple example below.
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA : NUM TYPE I VALUE 5.
METHODS : METH1 IMPORTING INPUT1 TYPE I .
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD : METH1.
num = NUM * INPUT1 .
WRITE:/5 NUM .
num = 5.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1.
CREATE OBJECT : OREF1.
Different ways of calling the method with one import parameter
<b> CALL METHOD OREF1->METH1 EXPORTING INPUT1 = 4.
CALL METHOD OREF1->METH1( INPUT1 = 5 ).
CALL METHOD OREF1->METH1( 6 ).</b>
Regards,
‎2007 Mar 16 10:22 PM
Thanks for all your response and i agree with Rich that there exist little confusion
when calling methods.
Thanks,
Bvan
‎2007 Mar 16 10:23 PM
‎2007 Mar 16 10:27 PM
‎2007 Mar 16 11:33 PM