I have a very strange issue occurring within a switch statement in C# inside of VS2013. The switch statement evaluates a string value and has several cases expecting specific strings. Some of the cases work when the corresponding expected string value is passed in, others don't (I have verified that the strings should match through debugging, yet they do not get evaluated as a match and the case block is not entered as expected). There are several obvious things that could affect this, most obvious perhaps would be the input string having different CultureInfo than the strings defined for each case. However, the strings that are being passed to the switch statement originate from within the same application/solution using hard coded string values (coming from a class library into a ascx.cs file in an ASP.Net 4.0 application, both created on the same machine). I have been able to use the 'default' case containing an if statement to detect the following to be true:
switch (input)
{
case "Expected Value 1":
//This case works
break;
case "Expected Value 2":
//This case does not work
break;
default:
if (input.ToUpperInvariant() == "EXPECTED VALUE 2")
{
//this evaluates to true
}
break;
}
I have only been able to detect the expected string value by using ToUpperInvariant() (as per if statement in default case above) which makes me suspect that some CultureInfo issue may be present. However, I don't understand how the string can be any different, in terms of CultureInfo, from those values that are working with the switch cases properly, especially when all the values passed in are coming from the same source list of strings (again these are hard coded, typed values created in the same code file, put in the same list of strings, within the same class library). Perhaps CultureInfo or some other problem with encoding isn't even the issue... I don't know. My team and I are totally perplexed with this. Before anyone suggests it, we are aware of the idea of using enum types with the switch case as a way to combat this, but currently we are more concerned with finding out how this can even be happening. Any ideas out there?