2

So if I have a switch with 3 cases, each case has duplicate local variables declared in them. You would think that the variables would be local to that case so you should be able to use the same name repeatedly. However, this doesn't appear to be the 'case'.

Apparently the other case blocks can see the variables in each other.

Okay, no big deal right? Except that when you try and access that variable that it can obviously see, it says it can't see it???

int index = list.SelectedIndex;

switch(index){

case(0):
   bool val = true;  //First declaration s'allll good
   if(val) //No issues here either obviously
      MessageBox.Show("Huh?");
   break;

case(1):
   bool val = true;  //Says it already exists??
   if(val) 
      MessageBox.Show("Huh?");
   break;
case(2):
   bool val3 = true; //Change the variable name so you can use it however,
   if(val)  //When you try to access the val in case 0 it says it doesn't exist????? 
      MessageBox.Show("Huh?");
   break;
}

Is there an obvious syntax fold in space time I am missing here?

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
  • 2
    A [little searching](https://www.google.com/search?q=c%23+switch+scope&oq=c%23+switch+scope&aqs=chrome..69i57j69i58j0l3.2956j0j7&sourceid=chrome&espv=210&es_sm=93&ie=UTF-8) might give you some answers. – crashmstr Dec 05 '13 at 14:18
  • 2
    @crashmstr don't chastise me for wanting instant gratification. Americans have a rich heritage of getting what they want the moment they dream it. It's part of my culture! – DotNetRussell Dec 05 '13 at 14:19
  • @AMR I actually LOL'd at that comment, though it's sad if you mean it. – CodeCaster Dec 05 '13 at 14:21
  • @AMR I was just pointing out that a simple search could give those instant results. – crashmstr Dec 05 '13 at 14:21
  • @crashmstr, but you have to read. Reading is a lost art form. Here you just wait for the answer to come to you! – Mike Perrenoud Dec 05 '13 at 14:22
  • @MichaelPerrenoud ...but then you have to read the answer :O – crashmstr Dec 05 '13 at 14:22
  • @crashmstr, but that's a lot less work!!! You know how many people (not speaking of you AMR) want a copy and paste answer. – Mike Perrenoud Dec 05 '13 at 14:23
  • lol it's all good. I know I phoned it in on this one. I just didn't think that it was going to be a common answer. Had I, I would have looked it up. – DotNetRussell Dec 05 '13 at 14:26

3 Answers3

2

The variables, in the IL, are defined to the scope of the switch, so you can't reuse them in the other case statements because it would redefine them.

Likewise, you still have to define the variables for each case (i.e. you've seen how even if one case has the variable the others can't actually leverage its definition).

The better approach, for you, is to define val outside the switch.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

Since cases are just labels, there's no scoping between cases -- they can see variables on the highest scope of the case, hence collisions on your val.

You can either move bool val outside of the switch, or you can enclose the cases in braces to scope it yourself, i.e.

case(0):
{
  bool val = true;
  if (val)
    MessageBox.Show("Huh?");
}
break;
Slate
  • 3,189
  • 1
  • 31
  • 32
-1

Variables in a switch statement are scoped to the entire switch statement. See this MSDN page at the bottom "The scope of a local variable or constant declared in a switch block is the switch block.".

To get around this, you can either declare the variable above the switch statement or (less cleanly) declare it a single time and re-use throughout the switch statement like so.

int index = list.SelectedIndex;

switch(index){

case(0):
   bool val = true;  //First declaration s'allll good
   if(val) //No issues here either obviously
      MessageBox.Show("Huh?");
   break;

case(1):
   val = true; //Declared in case 0
   if(val) 
      MessageBox.Show("Huh?");
   break;
case(2):
   val = true; //Still declared from case 0
   if(val) 
      MessageBox.Show("Huh?");
   break;
}
bpruitt-goddard
  • 3,174
  • 2
  • 28
  • 34