-1

I was wondering if there is a way to get the values of every case in a switch statement? When you provide a not implemented case, I would like to throw some exception and provide a list of available case values.

switch (partName.Trim().ToLower())
{
 case "engine":
 //something
 break;
 case "door":
 //something
 break;
 case "wheel":
 //something
 break;
 default:
 throw new NotImplementedException($"Available parts are {????}.");
}
asked Jan 11, 2019 at 8:48
2
  • I suppose you mean programatically? You could do it manually. Commented Jan 11, 2019 at 9:02
  • If you're trying to head off subtle coding errors where a case has been missed then tools like ReSharper can spot this if the switch statement is an enum. Commented Jan 11, 2019 at 9:04

2 Answers 2

7

Short answer: no. There is no way of doing this programmatically.

Longer answer: you can work around this with an enum, eg

public enum Parts { engine, door, wheel }
...
if (Enum.TryParse(partName, out Parts part))
{
 switch (part)
 {
 case Parts.engine:
 //something
 break;
 case Parts.door:
 //something
 break;
 case Parts.wheel:
 //something
 break;
 }
}
else
{
 var listOfValues = string.Join(", ", Enum.GetNames(typeof(Parts)));
 throw new NotImplementedException($"Available parts are {listOfValues}.");
}

This isn't a complete solution as I might forget to add a case for one of the enum values and I'll get a confusing error telling me that the value I supplied is supported when it's not. But that limitation aside, it will work if the switch is correctly implemented.

answered Jan 11, 2019 at 9:06
4
  • This does not solve the problem. If you decide not to list all enum values in the switch, your code will throw an exception even if you pass an actual enum value to the method and then tell you your choice was invalid, here is a list of valid choices which will contain the value you have used. Commented Jan 11, 2019 at 10:53
  • 1
    @Andy, I refer you to the first part of my answer: "Short answer: no. There is no way of doing this programmatically.. The work-around is just that: a work-around. It isn't a complete solution. I've updated my answer to explicitly state this. Commented Jan 11, 2019 at 10:57
  • @Andy Your criticism seems somewhat churlish given the limitations of the code provided. There may of course be better ways to solve the underlying problem - but any solution has to be pitched with the best interests of the OP which (for good or bad) starts with the code the OP presents us with. Commented Jan 11, 2019 at 11:22
  • 1
    @RobbieDee, not sure what's churlish about my comment. I initially downvoted this answer, because it was not complete without the warning that the solution has its limitations. I changed my vote to upvote once David has added the explanation, informing a user to be aware of the problem. This site is browsed by many programmers who don't have as much experience as David, you, or me, so even if something is obvious to us, it should be stated for those to whom it is not. Commented Jan 11, 2019 at 11:27
5

As has been already said, switch is of no help to you in this scenario. If you wanted, you could ditch the switch in favour of configured map with actions, e.g. (non-language-specific code):

Map<String, Action> transformations = Map.of(
 Pair("engine", EngineTransformation()),
 Pair("door", DoorTransformation()),
 Pair("wheel", WheelTransformation()),
);
var partName = partName.Trim().ToLower();
if (!transformations.contains(partName) {
 throw new NotImplementedException(
 $"Available parts are {}.",
 transformations.getKeys()
 );
}
var someResult = transformations.get(partName).getValue().execute();
answered Jan 11, 2019 at 11:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.