3

How do I jump to another case statement in a switch-case condition with the value of current case statement?

Is it possible to implement this kind of things using switch case or is there another way to implement it?

Is it possible to achieve? If not, then is there another way to achieve it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sss
  • 1,519
  • 8
  • 37
  • 67
  • It's not clear why you've got `goto` here. It doesn't help that there's so much context missing. Can you provide a short but *complete* example? What do you mean by "another function belonging to another case statement"? In what way does a function belong to a case? – Jon Skeet Jun 06 '14 at 15:18
  • @AnthonyRussell yes i did but it dont work. But i want any alternative if someone know – Sss Jun 06 '14 at 15:18
  • 1
    @JonSkeet If I understand correctly, he wants to hit into `case Combo` and if needed, jump into the `case Slider` based on the `if` statement. – TyCobb Jun 06 '14 at 15:19
  • @TyCobb exactly. I wanna jump case slider if i get "returnComboItemSelect=="Slider" but with returnComboItemSelect value. Is it possible to do that ? – Sss Jun 06 '14 at 15:20
  • @JonSkeet I think he is saying he wants to return a Func – DotNetRussell Jun 06 '14 at 15:36
  • @AnthonyRussell please see the edited code.I have removed the confusing part. Also please let me know if it is possible or you know any other way to achieve same thing ? – Sss Jun 06 '14 at 15:40
  • @TyCobb please see the edited code.I have removed the confusing part. Also please let me know if it is possible or you know any other way to achieve same thing ? – – Sss Jun 06 '14 at 15:41
  • @JonSkeet please see the edited code.I have removed the confusing part. Also please let me know if it is possible or you know any other way to achieve same thing ? – Sss Jun 06 '14 at 15:41

5 Answers5

5

This code works:

    switch (this.Name)
    {
    case "":
        if (this.Owner == null)
        {
            goto DoBill;
        }
        break;
    case "Bill":
    DoBill:
        break;
    }

However, anyone who actually does it should be shot. Or talked to very severely, at the least. Why not just do the sensible thing?

switch (param.Component.Type)
    {
        case "Combo":
            returnComboItemSelect = generateCB(param);
             if(returnComboItemSelect=="Slider")
             {
               returnSomething  = generateSl(param,returnComboItemSelect); //I mean no need to jump
             }
            break;                            
        case "List":
            returnSomething = generateL(param);
            break;
        case "Slider":
            returnSomething 
.....

Seriously, if you start jumping about between case statements then you shouldn't be using them. I'm not too keen on the code above either (duplication, but sometimes...)

simon at rcl
  • 7,326
  • 1
  • 17
  • 24
3

You could but a while loop around the switch statement and break out of the loop when you are done.

var keepLooping = true;
var switchCaseSwitch = param.Component.Type;

while(keepLooping)
{
    switch (switchCaseSwitch)
    {
        case "Combo":
            returnComboItemSelect = generateCB(param);
            if (returnComboItemSelect == "Slider")
            {
                switchCaseSwitch = "Slider";
            }
            else
            {
                keepLooping = false;
            }
            break;                            
        case "List":
            returnSomething = generateL(param);
            keepLooping = false;
            break;
        case "Slider":
            returnSomething = generateSl(param,1);
            keepLooping = false;
            break;
        case "RadioButtons":
            returnSomething = generateRB(param);
            keepLooping = false;
            break;
        case "CheckBox":
            returnSomething = generateCHB(param,flag);
            keepLooping = false;
            break;
        default:
            throw new Exception("Unknown component type");
     }
}
Jon Hunter
  • 882
  • 2
  • 8
  • 18
1

I think you would be FAR better off rethinking your architecture.

People dont tend to use GOTO in C# these days unless they are trying to break nested loops.

for example

foreach(var item in items){
   foreach(var name in item.people){
      if(name == WhatIAmSearchingFor)
        goto found;

   }
}
found:
   //Your Code

MSDN Reference on GOTO

Something as simple as returning a Func, and a property bag of params is a clearer way to do this. Or like I have in the example below just do your first param and then an enum or flag of some sort. Instead of passing an empty one just make it 0

Here is a S.O. answer on how to do this Can a C# method return a method?

So in your example you might do this

public Func<ParamType,int> YourMethodName{

    YourSwitch{
       case(1)
         return YourClassName.YourMethod;
       break;
       case(2)
         return YourClassName.YourMethod2;
       break
       case(3)
         return YourClassName.YourMethod3;
       break
     }
} 
Community
  • 1
  • 1
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
1

Or you could ditch the case statement and do something like that:

    //Setup
    var selector = new Dictionary<string, Func<Param, string, string>>();
    selector.Add("Combo", (p, flag) =>
        {
            var returnComboItemSelect = generateCB(p);
            if (returnComboItemSelect == "Slider")
            {
                return selector["Slider"](p, returnComboItemSelect);
            }
            return returnComboItemSelect;
        });
    selector.Add("List", (p, flag) => { return generateL(p); });
    selector.Add("Slider", (p, flag) => { return generateSL(p, flag); });
    selector.Add("RadioButtons", (p, flag) => { return generateRB(p); });
    selector.Add("CheckBox", (p, flag) => { return generateCHB(p, flag); });


    //use
    var result = selector[param.Component.Type](param, flag);
Markus Luedin
  • 409
  • 2
  • 12
1

You can use a goto case.

A common use case is when you want one case to run to the next case.

switch ( arg )
{
    case "-file":
        if ( (i + 1) < args.Length && !args[i + 1].StartsWith("-") )
            inputBox.Text = args[++i];
        break;
    case "-language":
        _loadSelectedLanguagesAsALanguageSet = true;
        goto case "-select";
    case "-select":
        if ( (i + 1) < args.Length && !args[i + 1].StartsWith("-") )
            _loadSelectedLanguageFileOnStartup = args[++i];
        break;
}