If have a problem to assign a value to a (array) variable in a switch statement.
I have a solution that works using "Temp" variables like myArrayTemp1 and myArrayTemp2. However I wonder why I can not use the following code.
I'm not sure if this has something to do with the scope of the variable...so here is my code :
int x;
x=1;
string[,] myArray = new string[2, 2];
switch (x)
{
case 1:
string[,] myArrayTemp1 = { { "1", "1" }, { "1", "1" } }; //is OK
myArray = myArrayTemp1; //is OK
myArray = { { "1", "1" }, { "1", "1" } }; //error
break;
case 2:
string[,] myArrayTemp2 = { { "2", "2" }, { "2", "2" } }; //is OK
myArray = myArrayTemp2; //is OK
myArray = { { "2", "2" }, { "2", "2" } }; //error
break;
}
MessageBox.Show ("myArray:" + myArray[0,0]);
I want to get rid of myArrayTemp1 and myArrayTemp2 and assign values to myArray in the case blocks. And I need to use the myArray = { { "1", "1" }, { "1", "1" } }; notation and not myArray[x,y] = "1"
thank you