tags:

views:

185

answers:

5

I would like to dynamically control code execution using a pointer, basically a function pointer without the stack frame. Execution does not return to the calling location, but to a single point.

TopOfLoop:
...
Jump(x)


x1:
...
continue

x2:
...
continue

etc.

Is this possible in c#? Thanks.

+2  A: 

Maybe by using goto statement?

Reference at MSDN: http://msdn.microsoft.com/en-us/library/13940fs2.aspx

Though, it's not recommended way of coding. Try to use delegates instead:

 Func<T,TResutl> or Action<T>

Sample:

using System;

public class LambdaExpression
{
   public static void Main()
   {
      Func<string, string> convert = s => s.ToUpper();

      string name = "RIA Guy";
      Console.WriteLine(convert(name));   
   }
}
Koistya Navin
No, not with goto. I believe pro3carp3 is asking about dynamic gotos, and C# does not support that. You would either need to use a switch statement, or a delegate in the way Koistya recommends.
Aaron
A goto is hard-coded. I want to jump to a code-pointer, but it appears c# doesn't allow it. Thanks.
pro3carp3
A: 

You could use goto. I wouldn't, but it'll do what you want.

Chris Doggett
+1  A: 

In terms of function pointers, you'd probably want to have a look at delegates in c#. As to the rest of it, that has been covered by others.

Martin Clarke
A function pointer still creates and returns from a stack frame which isn't what I was looking for.
pro3carp3
A: 

You can certainly invoke a delegate with a callback, but I'm not sure how to accomplish the "Execution does not return to the calling location" part outside of a goto.

Joel Coehoorn
+1  A: 

This also looks like a SWITCH would work, and it would avoid the "goto" in code (though it would probably compile the same).

John Gietzen