试图让所有的文本框的值到一维,二维阵列指数超出范围异常错误
int[] xMatrix = new int[6], yMatrix = new int[6];
int[,] aMatrix = new int[6, 6], bMatrix = new int[6, 6], cMatrix = new int[6, 6];
foreach (Control control in this.Controls)
{
if (control is TextBox)
{
string pos = control.Name.Substring(1);
if (control.Name.StartsWith("a"))
{
int matrixPos = Convert.ToInt32(pos);
int x = matrixPos/10;
int y = matrixPos % 10;
aMatrix[x, y] = Convert.ToInt32(control.Text);
}
else if (control.Name.StartsWith("b"))
{
int matrixPos = Convert.ToInt32(pos);
int x = matrixPos/10;
int y = matrixPos % 10;
bMatrix[x, y] = Convert.ToInt32(control.Text);
}
else if (control.Name.StartsWith("c"))
{
int matrixPos = Convert.ToInt32(pos);
int x = matrixPos/10;
int y = matrixPos % 10;
cMatrix[x, y] = Convert.ToInt32(control.Text);
}
else if (control.Name.StartsWith("x"))
{
int arrayPos = Convert.ToInt32(pos);
xMatrix[arrayPos] = Convert.ToInt32(control.Text);
}
else if (control.Name.StartsWith("y"))
{
int arrayPos = Convert.ToInt32(pos);
yMatrix[arrayPos] = Convert.ToInt32(control.Text); // <== ERROR LINE
}
}
收到错误消息
,这里是给定的值
我错过了什么?
好吧,我需要使所有数组+1 +1大。因为它们从0开始而不是1 – heron 2012-07-06 05:26:51
不,不要使数组1变大,使索引1变小。否则你浪费了第一个数组元素,索引0.请参阅我的答案以了解代码更改。 – 2012-07-06 05:28:07