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

How to make recursive call inside method

Former Member
0 Likes
2,309

I am learning OO and I tried to convert a function to method of global class.

In existing function, there is recuresive call of a sub routine defined in the function group.

Question is how can I make a recursive call of this routine inside my method.

I tried to create a local class inside my method M1 of global class A but got syntax issue.

So I created a private method in the class A and call this private method PM1 inside my method M1.

Is this the right approach? I just want this private method known to my method only, Not inside the class.

For example:

Funtion F1.

Perform  form1.

Form  Form1.

  Loop at itab into struct.

   Do something here.

   perform sub1.

   Do something more.

  endloop.

endform.

Now I convert to

Class A definition.

public section.

  method M1.

Endclass.

Class A implementation.

Method M1.

Loop at itab into struct.

    call method PM1a

endloop.

Endmethod.

Method PM1a  is private method inside class A.

Is there any better approach?

Thanks for your guidance.

4 REPLIES 4
Read only

Former Member
0 Likes
911

Hi Valerie,

that sounds like a reasonable approach, making the subforms private methods.

Regards

   Adi

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
911

The example you have written doesn't show any recursive calls. Recursive calls - calling the same routine internally in the called routine.

In your case Form1 calls sub1 - both are different, so not a recursive call. Just go through the design patterns available and choose the appropriate one.

You can find the best examples of design patterns here http://zevolving.com/category/abapobjects/oo-design-patterns/

Read only

Private_Member_49934
Product and Topic Expert
Product and Topic Expert
0 Likes
911

I gues in your code you meant to

Form  Form1.

  Loop at itab into struct.

   Do something here.

   perform Form1.

   Do something more.

  endloop.

endform.

I think your current approach is correct which as per your example.

Method M1.

Loop at itab into struct.

    call method PM1

endloop.

Endmethod.

Method PM1.

IF SOME CONDITION.

    call method PM1.

ELSE.

   EXIT.

ENDIF.

Endmethod.

Read only

Former Member
0 Likes
911

Hi,

In OOP ABAP from inside a method of a class you can call other instance methods of the same class whether public or private using keyowrd ME.

ME is a self refrencing variable of a class object created automatically by runtime. i.e it contains reference of the its own object.

thus you can use it like

Class A implementation.

Method M1.

Loop at itab into struct.

    ME->PM1a(  ).               "using self referencing variable to call method of its own object.

endloop.

EndMethod.

Method PM1a.

...

...

Endmethod.

Endclass.

Thank You.