I've been working with a switch statement
and it works fine. However, I want the case to be evaluated only once and if it comes again don't evaluate. Here's the code I tried, which works:
private static int total = 0;
private static string[] ErrorCode = new string[] { "@", "-2", "!" };
private static int Score(string[] errorCodes)
{
var sum = 0;
foreach (var ec in errorCodes)
{
switch (ec)
{
case "@":
sum += 1;
break;
case "-2":
sum += -2;
break;
case "!":
sum += 5;
break;
}
}
return sum; //This returns 4
}
But, if the string[]
array has a repeated value it adds the value, which evaluates again. Like this:
private static string[] ErrorCode = new string[] { "@", "-2", "!", "!" };
//This returns 9 (Because of "!") but would like to return 4
How can I achieve to evaluate "!"
only once, or should I take a different approach? Thanks for the help!