Reputation: 1529
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?
Upvotes: 4
Views: 11961
Reputation: 188
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;
}
Upvotes: 3
Reputation: 9857
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
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
}
}
Upvotes: 1
Reputation: 419
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);
Upvotes: 1
Reputation: 880
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");
}
}
Upvotes: 3
Reputation: 7344
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...)
Upvotes: 6