4

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

I need to assign some values to an array depending on a condition. So I can use "if" of "switch" statement for checking the condition.

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

And if I use "if" then I get an error "does not exist in 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 the array (1a)...but I can not access the array elements (1b)... Using "switch" doesn't work at all.

So how could I assign and then access values to an array depending on a condition (if/switch)?

I use Visual Studio 2010.

thanks

1
  • 2
    You declare arraySpielfeld2dtwo times in the scope of the switch. Commented Jan 6, 2015 at 13:36

4 Answers 4

6

What you're encountering here is the scope of the variables.

Any variable declared inside a block { } is only visible within that block. Any code after the block will not be able to see it. Thus, your if-using code declares the variable, but it does so inside those two branches so the code immediately afterwards can't see it at all.

The solution is to declare the variable before the if, assign it in the blocks, then you can use it afterwards (just make sure you don't leave a path where it can end up unassigned, unless you're prepared for that possibility when you use it).

The switch code doesn't work because there's only one block for the whole statement, and you declare the same variable name twice within it.

It still has the same scoping problem though, because that variable won't be visible outside the switch block. Again, the solution is to declare the variable first, then assign it within the switch.

Sign up to request clarification or add additional context in comments.

Comments

3

You have declared the array in the scope of the if, but you want to access it from outside. That doesn't work. You have to declare it outside. But then you can't 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 a switch which also creates a new scope if you use braces({ }).

Note that you don't need an if or switch in this case, it seems that you always want to use x:

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

4 Comments

The same holds true for the switch statement as well. Variable needs to be declared outside switch, then populated inside switch.
I liked the creative way of adapting the code so you can use the collection initializer syntax. +1
thank you for the answer. I get an error if I try to use this variant : if (x == 1) arraySpielfeld2d= { {"1", "1" }, {"1", "1" } }; }
@Spacewalker: i don't understand, why haven't you used the code that i've shown in my answer? I mention that you can't use collection initializer syntax(in one line) if you need to initialize it in another line(in the if or switch). So i've shown the way how to initialize it in the classic way. But also look at the last approach which always uses x.ToString() as value.
0

declare arraySpielfeld2d outside the if/else or switch scope then you can access it outside if/else and inside switch

Comments

0

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

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
  }

1 Comment

OK, but if I do the following int x; x=1; string[,] arraySpielfeld2d ; if (x==1) { arraySpielfeld2d= { {"1", "1" }, {"1", "1" } }; } I get "invalid }"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.