-3

I have a couple of nested methods in some class. First of them is called from main function. Now, if some condition is true, I want that next executed statement is some statement in main . How do that?

class A {
      void a()
        {
            b();
            c();
        }
        void b()
        {
            e();
            c();
        }
 }

If some condition in any function in class A is true, I want go to main function in class B, exactly on statement that is first after calling A.a()

Error
  • 815
  • 1
  • 19
  • 33
  • 5
    Could you please provide a code sample? Also, my first reaction is: you can't, but maybe I fail you understand what you're trying to achieve... – Thorsten Dittmar Jan 02 '13 at 15:38
  • 1
    Can you show some code that illustrates what you're trying to accomplish? (Even non-compiling pseudo-code?) It's not clear what you mean. It sounds like you just want to return from the first method. If that's the case, just use the `return` keyword. – David Jan 02 '13 at 15:38
  • 1
    Sounds like you want to pass a delegate as parameter. But some code would be helpful. – ken2k Jan 02 '13 at 15:38
  • 1
    Quite the assumption @ken2k; if I were to be so ignorant - I'd assume that based on the juniority of asking 'how to return to main function', OP is rather new to such principles and is probably talking about basic coding. I think we're all having an 'educated' guess at what you mean here OP, please post some code or, at least, provide a reworded explanation. –  Jan 02 '13 at 15:39
  • As you can tell from the answers so far, anybody can guess as to what you need. However, if you can post a code sample, we might be able to come up with more targeted advice. – David Jan 02 '13 at 15:54

2 Answers2

0

Use Goto identifier

Where Identifier is the point where you want to return to in main

stamhaney
  • 1,246
  • 9
  • 18
  • Wow, I didn't even know `goto` existed in C# until just now. I don't recommend its use for controlling the flow of execution, though. – David Jan 02 '13 at 15:41
  • I know not quite the solution, but the solution he asks warrants it – stamhaney Jan 02 '13 at 15:41
  • That answer is correct in terms of doing what you asked for. (Downvoting stamhaney is not really fair per se). However, goto is almost never a good idea. Take a look at some of the discussion here: http://stackoverflow.com/questions/46586/goto-still-considered-harmful – DWright Jan 02 '13 at 15:42
  • Ok guys just a suggestion, can remove it :) – stamhaney Jan 02 '13 at 15:43
  • 1
    another joke, can goto traverse across functions. That is i guess the intention of OP. – Tilak Jan 02 '13 at 15:43
  • 3
    `goto` can traverse time and space. – CAbbott Jan 02 '13 at 15:43
  • I tried goto before posting question here, and it doesnt't work. I get error that such label is not in goto scope. And I am not sure that goto can jump to label in another class. – Error Jan 02 '13 at 15:51
0

If you want to execute something in main based on a function then use Func<bool>, if your function takes in a parameter then use Func<T in, bool>.

Usage

  var isInt2 = new Func<int, bool>(int i => i == 2);
  if (isInt2(2))
    Console.WriteLine("Do something in main");

After your lambda => you do your condition. The above is an example of Func<T in, bool>.

http://msdn.microsoft.com/en-us/library/bb549151.aspx

LukeHennerley
  • 6,344
  • 1
  • 32
  • 50
  • Pretty sure the outside function isn't where the boolean should come from, but he may want to use delegates here. My interpretation is that he wants to pass an `Action` from main to this class, which will come up with a boolean from...somewhere, and if true will call that action. – Servy Jan 02 '13 at 16:35