試圖讓所有的文本框的值到一維,二維陣列指數超出範圍異常錯誤
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