-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 {????}.");
}

closed as off-topic by gnat, Jörg W Mittag, Robbie Dee, amon, Greg Burghardt Jan 11 at 13:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for assistance in explaining, writing or debugging code are off-topic here. These can be asked on Stack Overflow if they include the desired behavior, a specific problem or error, and the shortest code necessary to reproduce it in the question (see Minimal, Complete, and Verifiable Example)." – gnat, Jörg W Mittag, Robbie Dee, amon, Greg Burghardt
If this question can be reworded to fit the rules in the help center, please edit the question.

  • I suppose you mean programatically? You could do it manually. – Phil N DeBlanc Jan 11 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. – Robbie Dee Jan 11 at 9:04
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.

  • 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. – Andy Jan 11 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. – David Arno Jan 11 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. – Robbie Dee Jan 11 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. – Andy Jan 11 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();

Not the answer you're looking for? Browse other questions tagged or ask your own question.