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: 
Read only

ABAP OOP / Calling Method ...Help

Former Member
0 Likes
14,977

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

9 REPLIES 9
Read only

RichHeilman
Developer Advocate
Developer Advocate
4,539

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

Read only

Former Member
0 Likes
4,539

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

Read only

0 Likes
4,539

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...

Read only

Former Member
0 Likes
4,539

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.

Read only

Former Member
0 Likes
4,539

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,

Read only

0 Likes
4,539

Thanks for all your response and i agree with Rich that there exist little confusion

when calling methods.

Thanks,

Bvan

Read only

Former Member
0 Likes
4,539

Thanks

Read only

0 Likes
4,539

Please be sure to award points if any of these answers were a little help. I know it is confusing.

Regards,

RIch Heilman

Read only

0 Likes
4,539

Note: The others have provided pretty good answers, much more so than mine. If they were helpful, please consider awarding points there as well. Thanks

Regards,

Rich Heilman