0

It's just a simple code but I don't know where I'm missing quotations or etc
My error lies here: case "000\";

Any help is appreciated! Thanks!

 MatchCollection matches1 = Regex.Matches(strFields[3], @"\d{3}\\");
                                            string range = matches1[0].Value;
                                            switch (range)
                                            {
                                                case "000\";
                                                    strAdType += "A";
                                                    break;
                                                case "001\":
                                                    strAdType += "B";
                                                    break;
                                                case "002\":
                                                    strAdType += "C";
                                                    break;
                                                case "003\":
                                                    strAdType += "D";
                                                    break;
                                                case "004\":
                                                    strAdType += "E";
                                                    break;

                                            }
3
Contributors
3
Replies
8
Views
4 Years
Discussion Span
Last Post by Jahliah
0

You can't use strings in case statements -- only integers and single characters. If you need strings the only way to do it is with a series of if/else
statements.

"000\";

That is also wrong because the compiler will attempt to use \ as an escape character and out " as part of the string iteself. What you want is one of these two

"000\\"; // make \ part of the string

"000\""; // make " part of the string

Edited by Ancient Dragon

0

You most assuredly can use string in case statements. The restriction is that the values must be constants at compile time.

Your problem is that the \ character is an escape character and it thinks you are looking for 000" and there is no closing quote for the string so the rest of the code is garbage. Either double up the \ or prefix it with an @

case "000\\": blah; break;
case @"001\": blah2; break;

And please tell me you aren't building strings by appending characters. Use StringBuilder for that.

Edited by Momerath

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.