使用二维数组变量在switch语句C#错误 [英] C# error in switch statement using an 2d array variable
问题描述
如果有一个值赋给(阵列)的问题在switch语句变量。
If have a problem to assign a value to a (array) variable in a switch statement.
我有使用类似myArrayTemp1和myArrayTemp2TEMP变量有效的解决方案。
但是我不知道为什么我不能使用下面的code。
I have a solution that works using "Temp" variables like myArrayTemp1 and myArrayTemp2. However I wonder why I can not use the following code.
我不知道,如果这事做与变量的范围......所以这里是我的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]);
我想摆脱myArrayTemp1和myArrayTemp2和分配值的情况下块MYARRAY。
我需要使用myArray的= {{1,1},{1,1}};符号和不myArray的[X,Y] =1
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"
感谢您
推荐答案
您只需要创建新的数组:
You just need to create the new array:
string[,] myArray = new string[2, 2];
...
myArray = new string[2,2] { { "1", "1" }, { "1", "1" } };
这篇关于使用二维数组变量在switch语句C#错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!