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
arraySpielfeld2dtwo times in the scope of theswitch.