1

Is there a way to use a switch statement to set the value of one particular variable used throughout the cases? At the moment if value is 1, I want variable to be 1, and if value is 2, I want variable to be 2. I am actually doing it simply by:

Int variable = value;

..however, for future reference where an example is more complex, I wondered if there is a way. (currently getting a variable is already defined error).

  switch (value)
        {
            case 0:
                int Variable = 0;
                break;
            case 1:
                int Variable = 1;
                break;
            case 2:
                int Variable = 2;
                break;
            case 3:
                int Variable = 3;
                break;
            case 4:
                int Variable = 4;
                break;
        }
user1166981
  • 1,738
  • 8
  • 26
  • 44
  • 3
    All the cases within a `switch` are all in the same scope. Just put your variable declaration outside of it and assign to it normally. – Jeff Mercado Jun 26 '12 at 00:43
  • Ahh, so obvious now you point it out, I was declaring the variable over and over, thanks alot! – user1166981 Jun 26 '12 at 00:44
  • 4
    Are you paid by the number of lines of code you write? – Steve Wellens Jun 26 '12 at 00:46
  • Seems a bit weird to use 20 lines when 1 works. – Steve Wellens Jun 26 '12 at 00:47
  • I am using 1 line of code... what are you asking? – user1166981 Jun 26 '12 at 00:48
  • He means why use a switch statement to assign the same value that value contains. Just keep using int Variable = value unless you know that you will eventually assign a different result to Variable. Oh, and read a bit on naming conventions. – JohnP Jun 26 '12 at 00:49
  • No I meant I wanted to learn how to do it where for example it is more complex such as if I have a string in one variable and want to set another variable to a specific int based on the text, if you know what I mean..(for future reference) – user1166981 Jun 26 '12 at 00:51

2 Answers2

5

How about a hashtable or dictionary. It's troublesome but quite readable i guess.

Idea taken from here.

Dictionary<string, int> numbers = new Dictionary<string, int>(){
    {"zero", 0},
    {"one", 1},
    {"two", 2},
    {"three", 3},
    {"four", 4},
    {"five", 5},
    {"size", 6}
};

int Variable = numbers[value];
Community
  • 1
  • 1
viclim
  • 959
  • 8
  • 16
2

If you declare your variable outside the swtich, you won't get the "variable already defined" error. This is happening because everything inside the switch is in the same scope.

Example:

int Variable = -1;

switch (value)
{
        case 0:
            Variable = 0;
            break;
        case 1:
            Variable = 1;
            break;
        case 2:
            Variable = 2;
            break;
        case 3:
            Variable = 3;
            break;
        case 4:
            Variable = 4;
            break;
}
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Icarus
  • 63,293
  • 14
  • 100
  • 115
  • An `else` clause would be a worthwhile addition to handle any unexpected values. A possible alternative is to use a nullable type, e.g. `int? Variable;`. – HABO Jun 26 '12 at 02:22