我正在创建一个程序,该程序从Excel工作表中导入信息的二维对象数组。然后它将这个数组传递给ProcessObjects方法以进行处理和打印/导出回excel模板。任何人都可以告诉我为什么会收到此错误消息?System.IndexOutOfRangeException - 索引超出了数组的范围
“类型‘System.IndexOutOfRangeException’的未处理的异常发生在Project.exe
其他信息:索引阵列的边界之外。”
private void ProcessObjects(object[,] classesArray, object[,] classesAvailabilityArray, Excel.Workbook workbook2, Excel.Sheets excelSheets)
{
// once classes are selected, they are copied to a temporary location
// while they're waiting to be printed
object[,] tempArray = new object[6,3];
// This stops the while loop once enough credit hours have been taken
// It must reach 123 hours for CS Degree .
int hourCounter = 0;
int iteration = 0;
while (hourCounter < 123)
{
// this while loop copies some classes from classesArray to tempArray
// so they can be printed into the excel template (NewStudentTemplateCS.xlsx)
//
int classes = 0, hours = 0; // stops while loop if limit is reached
int w = 0, x = 0; // used to select individual elements of tempArray (0 based)
// w = row
// x = column
int y = 1, z = 1; // used to select individual elements of classesArray (1 based)
// y = row
// z = column
while(classes < 7 || hours < 17)
{
// this loop checks the status of the flag and stops at the first avaliable
// class/row of classesArray
while (classesArray[y,7] == (object)1)
{
y++;
}
// copies the call EX: "MATH 2313" from classesArray to tempArray
tempArray[w,x] = classesArray[y,z];
x += 2;
z += 2;
// copies the name EX: "Calculus I" from classesArray to tempArray
tempArray[w, x] = classesArray[y, z];
x++;
z++;
// Copies the hours EX: "3" from classesArray to tempArray
tempArray[w, x] = classesArray[y, z];
Console.WriteLine("debug test");
// increments classes, hours, and hourCounter for exit decision
classes += 1;
hours += (int)classesArray[y, z];
hourCounter += (int)classesArray[y, z];
// sets flag to one
z += 3;
classesArray[y, z] = 1;
}// end while loop
// print method that prints temp array and clears tempArray for next use
PrintArray(tempArray, iteration, workbook2, excelSheets);
// iterates iteration
iteration++;
} // end while loop
// print method that prints temp array and clears tempArray for next use
PrintArray(tempArray, iteration, workbook2, excelSheets);
// iterates iteration
iteration++;
} // end while loop
} // end ProcessObjects method
我注释掉各自独立地以下行的,但是代码的每一行中的所有返回我上面列出的相同的错误。
Console.WriteLine("debug test");
// increments classes, hours, and hourCounter for exit decision
classes += 1;
hours += (int)classesArray[y, z];
hourCounter += (int)classesArray[y, z];
// sets flag to one
z += 3;
classesArray[y, z] = 1;
那么,如果你调试它会发生什么?是否有一些原因让你无法在代码的第一行中断,然后单步执行,运行到更多的断点或运行到光标,以找出问题出在哪里的索引?我们不知道您使用的是哪种测试数据。只需调试它! – 2012-04-24 23:59:37
从你的问题我们只能告诉tempArray的数组维度,我的猜测是classesArray不够大。向我们展示如何声明和填充classesArray? – 2012-04-25 00:05:18