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

Call an instance method inside another method of class

Former Member
0 Likes
21,416

Dear experts,

Kindly guide me how to call an instance method inside a other class method.

i am already using the static method from same class. Pls refer the code

   CALL METHOD /sapsrm/cl_pdo_msg=>/sapsrm/if_pdo_msg_provider~add_message_static
        EXPORTING
          it_messages        = lt_messages_tend
        CHANGING
          co_message_handler = co_message_handler.

In the above code /sapsrm/if_pdo_msg_provider is the class where add_message_static is a static method.

But i want to call method DELETE_ALL_MESSAGES which is an instance method in same class /sapsrm/if_pdo_msg_provider.

Kindly guide me how to call the instance method of the class.

Thank & Regards

Arun.K.P

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
8,610

Hi Arun,

  You should first create or get an instance in order to access the instance method.

Do you have get_instance( ) method for the class /sapsrm/if_pdo_msg_provider?

If so then do the following:

DATA:

   lr_msg_provider TYPE REF TO /sapsrm/if_pdo_msg_provider.

lr_msg_provider = /sapsrm/if_pdo_msg_provider=>get_instance( ).

If there is no get_instance method, the do the following:

CREATE DATA lr_msg_provider.

IF lr_msg_provider IS BOUND.

    CALL METHOD lr_msg_provider->delete_all_messages( ).

ENDIF.

The get instance method will create an instance if the instance in not created before, it the instance is created then it return the existing instance, so that the data can be preserved, you wont be having different set of data's.

Dear experts,

Kindly guide me how to call an instance method inside a other class method.

i am already using the static method from same class. Pls refer the code

   CALL METHOD /sapsrm/cl_pdo_msg=>/sapsrm/if_pdo_msg_provider~add_message_static
        EXPORTING
          it_messages        = lt_messages_tend
        CHANGING
          co_message_handler = co_message_handler.

In the above code /sapsrm/if_pdo_msg_provider is the class where add_message_static is a static method.

But i want to call method DELETE_ALL_MESSAGES which is an instance method in same class /sapsrm/if_pdo_msg_provider.

Kindly guide me how to call the instance method of the class.

Thank & Regards

Arun.K.P

14 REPLIES 14
Read only

Former Member
0 Likes
8,610

Hello,

       To call an instance method of a class you have to create an object of that class. In your case it is /sapsrm/if_pdo_msg_provider .

Read only

PeterJonker
Active Contributor
0 Likes
8,610

I don 't have the class /sapsrm/if_pdo_msg_provider in my SAP system, but basically you need either to instantiate an object by creating a variable reference to the class and instantiate it by calling the create method.

Or, the class probably has a static method get_reference_by_handler or something similar e.g. get_instance, since you are receiving back a handle (a reference) to the class instance.

Use that one to get a refernce to the instance and call the method

Example

Data: gr_msg_provider type ref to /sapsrm/if_pdo_msg_provider.

gr_msg_provider = call mehod /sapsrm/if_pdo_msg_provider=>get_reference.

CALL METHOD gr_msg_provider->DELETE_ALL_MESSAGES

Read only

Former Member
0 Likes
8,611

Hi Arun,

  You should first create or get an instance in order to access the instance method.

Do you have get_instance( ) method for the class /sapsrm/if_pdo_msg_provider?

If so then do the following:

DATA:

   lr_msg_provider TYPE REF TO /sapsrm/if_pdo_msg_provider.

lr_msg_provider = /sapsrm/if_pdo_msg_provider=>get_instance( ).

If there is no get_instance method, the do the following:

CREATE DATA lr_msg_provider.

IF lr_msg_provider IS BOUND.

    CALL METHOD lr_msg_provider->delete_all_messages( ).

ENDIF.

The get instance method will create an instance if the instance in not created before, it the instance is created then it return the existing instance, so that the data can be preserved, you wont be having different set of data's.

Read only

0 Likes
8,610

Hi Peter and Satish,

Thank you for the responses. I tried both the solutions but i am getting same error

"/SAPSRM/IF_PDO_MSG_PROVIDER=>GET_INSTANCE(", a field, a function or  "(" expected.

Could you please guide me..

Thanks & Regards

Arun.K.P

Read only

0 Likes
8,610

Hi,

Check the get_instance method if it expects any input value.

Cheers,

Arindam

Read only

0 Likes
8,610

Hi,

  I think get_instance( ) method is taking some importing parameters, check the get_instance( ) method and pass the proper values for getting the instance.

Read only

0 Likes
8,610

Use the pattern function to create the call to the method. Then you know which parameters you need to supply

Read only

0 Likes
8,610

Hi Satish and Arindam,

I checked that i dont have any instance_method here.. pls correct me if i am wrong and i dont have any parameters.

Thanks & Regards

Arun.K.P

Read only

0 Likes
8,610

Hi,

  Then you should create an reference manually.

DATA:

   lr_msg_provider TYPE REF TO /sapsrm/if_pdo_msg_provider.

CREATE DATA lr_msg_provider.

IF lr_msg_provider IS BOUND.

    CALL METHOD lr_msg_provider->delete_all_messages( ).

ENDIF.

Any can you explain your business scenario so that we can understand you proper requirement.

Read only

0 Likes
8,610

Hi,

Check the class in SE24. I dont have system access now you must be having constructor then CREATE OBJECT should create an instance of the class. Also a quick way would be to do a where used list of the class and check its usage in system.

Cheers,

Arindam

Read only

0 Likes
8,610

Dear Satish,

I done what you have suggested but i am getting

Only types of the form "REF TO value_type" are permitted in "CREATE  DATA".

Read only

0 Likes
8,610

Hi,

Try with this code:

DATA:

   lr_msg_provider TYPE REF TO /sapsrm/if_pdo_msg_provider.

CREATE OBJECT lr_msg_provider.

IF lr_msg_provider IS BOUND.

  CALL METHOD lr_msg_provider->delete_all_messages( ).

ENDIF.

If you dont know the parameters then, use "Pattern", select ABAP Objects Pattern,

Provide the following information:

Instance: lr_msg_provider

Class/Interface: /sapsrm/if_pdo_msg_provider

Method: Constructor

Read only

0 Likes
8,610

Hi Satish,

I am able to call the instance method using

data: lr_msg_provider type ref to /sapsrm/if_pdo_msg_provider.
CALL METHOD lr_msg_provider->DELETE_ALL_MESSAGES.

Thank you for your guidance.

Thanks & Regards

Arun.K.P

Read only

arindam_m
Active Contributor
0 Likes
8,610

Hi,

For using instance methods you must instantiate the class. Its not readily available as static methods. Do a CREATE OBJECT or TYPE REF TO and create a instance. Check the link below

http://help.sap.com/saphelp_nw70/helpdata/en/c3/225b5f54f411d194a60000e8353423/content.htm

Cheers,

Arindam