Jump to content
  • Advertisement
Sign in to follow this  
ryt

Branching in switch statement

This topic is 2086 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to jump from a switch statement to another switch statement without leaving the current switch in C# ?

Share this post


Link to post
Share on other sites
Advertisement

I don't think so. Why would you want to do this? Doesn't sound like a very good thing for code readability. 

Share this post


Link to post
Share on other sites
No. If you jump to another switch, you will leave your current switch by definition. And you won't be able to jump back because two switch statements cannot both contain labels that are in scope of the other.

It sounds like a really bad idea to me either way. Edited by Nypyren

Share this post


Link to post
Share on other sites

You should be able to embed a switch in the code block of a case label if that is what you mean to do.

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int condition = 0;
            switch (condition)
            {
                case 0:
                    {
                        switch (condition)
                        {
                            case 0:
                                Console.Write("Switch in a Switch");
                                break;
                        }
                    }
                    break;
                default:
                    break;
            }
        }

Share this post


Link to post
Share on other sites

If you want to re-use code, just factor it out to a method and invoke it from any location that needs to (re)use the functionality.

Edited by dmatter

Share this post


Link to post
Share on other sites

Probably I asked the question wrong so it is misunderstood. What I actually wanted was to jump from one case statement to another case statement in one switch like:

switch(somevalue)
{
case somevalue1:
	Function1();
	break;
case somevalue2:
	Function2();
	break;
}

So for example that the code jumps from case somevalue1 to case somevalue2 without leaving the switch statement.

But in C# we must have a break I think. Anyway I would like to jump to somevalue2 without deleting the break.

Share this post


Link to post
Share on other sites

What I actually wanted was to jump from one case statement to another case statement in one switch...


Yes, you can do that! And unlike the above examples, it's perfectly acceptable to do this if you want to:
 
switch(somevalue)
{
case somevalue1:
	Function1();
	goto case somevalue2;

case somevalue2:
	Function2();
	break;
}
Edited by Nypyren

Share this post


Link to post
Share on other sites

I didn't know that. It looks like an old basic code smile.png. Is this a good coding practice ?

Edited by ryt

Share this post


Link to post
Share on other sites

It's not that bad.  I haven't found myself needing to use it at all.  If you find yourself needing to do it a lot, it might indicate that you should refactor the common code out into a function.

Share this post


Link to post
Share on other sites

I was building a finite state machine where I don't need a while statement but instead I just needed to switch to another state right after the first one finished.

How would you transform the current code to put it out in a function ?

Edited by ryt

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!