C #: error using if / switch: "Local variable already defined in this scope"

I'm new to C # and my problem is probably simple, but I don't understand:

I need to assign some values ​​to an array based on a condition. So I can use an "if" for a "switch" statement to test the condition.

However, if I use "switch" then I get the error "Local variable already defined in this scope".

And if I use "if" then I get the error "does not exist in the current context"

Example:

int x;
x=1;

//1:
//a)  does work...
if (x==1)
{
        string[,] arraySpielfeld2d = 
        {
         {"1", "1" },
         {"1", "1" }
        };
}
else
{
        string[,] arraySpielfeld2d = 
        {
         {"1", "1" },
         {"1", "1" }
        };
}


//b) but this does not work
MessageBox.Show(arraySpielfeld2d[0,0]);  //error: does not exist in current context


//2) doesn't work at all
switch (x)
    {

    case 1:
        string[,] arraySpielfeld2d = 
        {
         {"1", "1" },
         {"1", "1" }
        };
        break;


    case 2:
        string[,] arraySpielfeld2d =    //error:Local variable already defined in this scope
        {
         {"2", "2" },
         {"2", "2" }
        };
        break;

    }

      

So using "if" I can at least populate array (1a) ... but I cannot access the elements of array (1b) ... Using "switch" doesn't work at all.

So how can I assign and then get the values ​​in the array depending on the condition (if / switch)?

I am using Visual Studio 2010.

thank

+3


source to share


4 answers


What you see here is the scope of variables.

Any variable declared inside a block { }

is visible only within that block. Any code after the block will not be able to see it. So your if

-using code is declaring a variable, but it does it inside those two branches, so the code right after that can't see it at all.

The solution is to declare the variable before if

, assign it in blocks, then you can use it afterwards (just make sure you don't leave a path where it might end up unassigned if you're not prepared for this possibility when you use it) ...



The code switch

doesn't work because there is only one block for the whole statement and you are declaring the same variable name in it.

It still has the same scoping problem because that variable won't be visible outside the block switch

. Again, the solution is to first declare the variable and then assign it to switch

.

+4


source


You have declared an array in scope if

, but you want to access it from the outside. This does not work. You have to declare it outside. But then you cannot use the collection initializer syntax:

int x;
x=1;
string[,] arraySpielfeld2d = new string[2,2];

if (x == 1)
{
        arraySpielfeld2d[0,0] = "1";
        arraySpielfeld2d[0,1] = "1";
        arraySpielfeld2d[1,0] = "1";
        arraySpielfeld2d[1,1] = "1";
}
else if(x == 2)
{
    arraySpielfeld2d[0, 0] = "2";
    arraySpielfeld2d[0, 1] = "2";
    arraySpielfeld2d[1, 0] = "2";
    arraySpielfeld2d[1, 1] = "2";
}
MessageBox.Show(arraySpielfeld2d[0,0]);

      

The same is true for switch

, which also creates a new region if you use curly braces ( { }

).



Note that in this case you do not need if

or switch

seem to always want to use x

:

string val = x.ToString();
string[,] arraySpielfeld2d = 
{
     {val, val },
     {val, val }
};

      

+3


source


declare arraySpielfeld2d

outside the scope if/else

or switch

, then you can access it outside if/else

and insideswitch

0


source


This is how your code should start, basically you have locked the scope of the variable inside the if-then-else, it needs to be declared outside the parentheses.

int x;
x=1;
// define variable OUTSIDE of the if-then-else scope
string[,] arraySpielfeld2;
or
string[2,2] arraySpielfeld2 = new string[2,2]();

if (x==1)
{
        // assign the variable here if you create it then scope is
        // restricted to the code between if-then-else
        // unlike javascript you can't define a global
        // variable here (by leaving off the var part)
        arraySpielfeld2d = ... snipped
  }

      

0


source







All Articles