Wednesday, March 20, 2013

Method can be called from the same Method


A method can call itself by Call Method statement inside the implementation of the method. Inside there method will have to be declared directly as CALL METHOD METH. That means object will not be mentioned here. Important point is that the method must have an EXIT point to get outside of the method. Otherwise the method will fall in an infinite calling/looping.

Below is a program where we create a numeric table. Here we are getting the number by input parameter and multiplying it with the count. This count is initially 0 and it is gradually increased by 1. And the multiplied value is stored in a public variable and then it is written. Hence we are having the numeric table of input value. Inside the method we have called an Exit point. If the count is greater than 20 then it will get outside from the method otherwise it will continue for calling the method.

REPORT  zsr_test NO STANDARD PAGE HEADING.

PARAMETERS p_num TYPE i.

*----------------------------------------------------------------------*
*       CLASS cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
  PUBLIC SECTION.
    DATA: count TYPE i,
          value TYPE i.
    METHODS m_tab.
ENDCLASS.                    "cls DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
  METHOD m_tab.
    count = count + 1.
    IF count GT 20.
      EXIT.
    ELSE.
      value = count * p_num.
      WRITE: / p_num, '*', count, '=', value.
      CALL METHOD m_tab.
    ENDIF.
  ENDMETHOD.                    "m_tab
ENDCLASS.                    "cls IMPLEMENTATION

START-OF-SELECTION.

  DATA obj TYPE REF TO cls.
  CREATE OBJECT obj.

  WRITE: /10 'Table of ', p_num, ':',
         /10 '-----------------------'.
  SKIP.
  CALL METHOD obj->m_tab.

The program generates the following output.



We can call different method by this similar technique. 

PARAMETERS p_text TYPE char40.
CLASS cl_test DEFINITION.
  PUBLIC SECTION.
    METHODSm_wrt1m_wrt2.
ENDCLASS.

CLASS cl_test IMPLEMENTATION.
  METHOD m_wrt1.
    WRITE 'Method 1'.
    CALL METHOD m_wrt2.
  ENDMETHOD.

  METHOD m_wrt2.
    WRITE'Parameter:'p_text.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA obj TYPE REF TO cl_test.
  CREATE OBJECT obj.
  CALL METHOD obj->m_wrt1.



 

7 comments:

virat said...

Very nice example

Anonymous said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!

Unknown said...



Best SAP Success Factors Training Institute in Chennai
http://thecreatingexperts.com/sap-successfactors-training-in-chennai/
Best SAP MM Training in Chennai
http://thecreatingexperts.com/sap-mm-training-in-chennai/
Best SAP SD Training in Chennai
http://thecreatingexperts.com/sap-sd-training-in-chennai/
http://thecreatingexperts.com/sap-hr-training-in-chennai/
Best SAP FICO Training in Chennai
http://thecreatingexperts.com/sap-fico-training-in-chennai/
Best SAP ABAP Training in Chennai
http://thecreatingexperts.com/sap-abap-training-in-chennai/
Best SAP BASIS Training in Chennai
http://thecreatingexperts.com/sap-basis-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

Camelliacanan said...

Very nice post here thanks for it I always like and search such topics and everything connected to them. Keep update more information..

SAP HR Training in Chennai

SAP SD Training in Chennai

SAP Basis Training in Chennai

Business World said...


Great information. the information provided is of great use as i got to learn new things. If you are looking for real time training in SAP ABAP, kindly contact sieve software. Sieve software offers real time training by industry experts with live projects and placement support.
SAP ABAP TRAINING IN HYDERABAD

Unknown said...

thank you for sharing nice information really very nicesap hr online classes

Sourav Rai said...

sir , why paramater is not imported inside the method, you have used it directly is it a good approach , and can we do in all cases when fetching data based on the paramater from multiple tables