1

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?

zanderwel
  • 155
  • 3
  • 7
  • [`case` statements are case-sensitive](http://rextester.com/DKOA28765) – Jamiec Mar 06 '15 at 11:58
  • "I have verified that the strings should match through debugging" - do you mean just visually? There are all kinds of things that could go wrong there, in terms of characters which *look* the same but aren't actually. Note that a string doesn't have a CultureInfo... it's just a sequence of characters. You might want to paste both your case value and the real value into the box in http://tinyurl.com/unicode-explorer and see if there are any unexpected characters. – Jon Skeet Mar 06 '15 at 11:58
  • A `switch` does an ordinal comparison so if you've got text with diacritics and you expect the values to be equal, you might be interested in this answer: http://stackoverflow.com/a/2334241/996081 – cbr Mar 06 '15 at 11:59
  • I have verified visually that the characters within the string are the same, but also, the string defining the case and the string from the source should be the same ass I have tried typing them both manually, and copy/pasting them both from the same source in note pad or from within the editor. – zanderwel Mar 06 '15 at 12:00
  • 1
    @zanderwel If you turn the strings into char arrays with `String.ToCharArray()` and debug to see the array's values in hexadecimal, are they still the same? Also, in addition to Jon's link, [this tool](http://rishida.net/tools/conversion/) is really useful for debugging all sorts of strings and characters. – cbr Mar 06 '15 at 12:03
  • I have compared character arrays, byte arrays, and they do not evaluate as equal in the switch statement, but in an if statement they will be true. I tried using the Unicode conversion tool you just provided, comparing both strings, they appear to be identical in all formats. – zanderwel Mar 06 '15 at 12:08
  • @zanderwel Can you join me in the chat? http://chat.stackoverflow.com/rooms/72404/grawcube-assisting-zanderwel – cbr Mar 06 '15 at 12:18
  • The switch case's string literal contained an UTF-8 BOM which is why this was failing. http://chat.stackoverflow.com/transcript/72404 – cbr Mar 06 '15 at 13:18

0 Answers0