Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there...

What exactly will the return do in this case...
C#
private void Method1 ()
{
   //some code here
   Method2(var1,var2);
}

private void Method2 (string var1,string var2)
{
    counter ++;
    
    if(counter < 3)
    {
       if(condition1 == condition2)
       {
           counter = 0;
           Method3();
           return;
       }
       if(condition1 != condition2)
       {
           //go back to method1 and try again
           Method1();
       }
    }
}
Posted
Updated 9-Jan-12 20:45pm
v3
Comments
Ganesan Senthilvel 10-Jan-12 2:42am    
Are you looking for the logic or functionality of return?
SharksRTB 10-Jan-12 2:46am    
Well.... Both? I want to go straigt to method3 if(condition1 == condition2) is true.
Sergey Alexandrovich Kryukov 10-Jan-12 2:56am    
OK, I answered how return works, but as to functionality -- you need to understand one big chunk of computer functionality. I prefer the "gray box" method; black box does not work effectively. Please see my answer.
--SA

From the answers you've see so far, you could not get a right idea of return. Try to read and understand this:
http://en.wikipedia.org/wiki/Call_stack[^].

—SA
 
Share this answer
 
Comments
Mehdi Gholam 10-Jan-12 3:05am    
5'ed
Sergey Alexandrovich Kryukov 10-Jan-12 3:15am    
Thank you, Mehdi.
--SA
Mehdi Gholam 10-Jan-12 3:14am    
Congratulations on your MVP!
Sergey Alexandrovich Kryukov 10-Jan-12 3:15am    
Thank you very much. You too!
--SA
Mehdi Gholam 10-Jan-12 3:33am    
Cheers Sergey!
Hi, return statement here means go out from Method2
 
Share this answer
 
Comments
SharksRTB 10-Jan-12 2:47am    
Yes... go out from method2... But does it mean go out of method2 and return to the caller which is method1?
SharksRTB 10-Jan-12 2:48am    
Because if condition1 == condition2 is true, i dont want to go back to method1 and execute again, i just want to get out?
Robby Tendean 10-Jan-12 2:54am    
Agree with Mehdi Gholam, it will return you to Method1() and continue to run code below Method2(var1,var2); but since your code has nothing there so eventually it will go out from Method1() as well.
A return statement means relinquish control of the currently executing code to the calling method, i.e. exit what you are doing in this method and go back to the next statement in who ever was calling from.

In your case the return in Method2 will go back to Method1 and since there is no more code in Method1, Method1 will finish also.
 
Share this answer
 
Comments
SharksRTB 10-Jan-12 2:54am    
You are indeed correct, and i agree with you.. See, if(condition1 != condition2) is true, I want to go back to method1 and execute the code again, subsequently, entering method2 again. But I only want to do this 2 times, hence my if statement on top , if(counter < 3). But if(condition1 == condition2) is true, i want to go to method3 and get out. Now in my code most of the times if(condition1 == condition2) is true, but then it goes to method3 twice...
Sergey Alexandrovich Kryukov 10-Jan-12 2:54am    
This is hard to explain in two short paragraphs. I'm afraid, the whole idea remains unexplained.
I found appropriate short article, please see my answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900