2012-04-24 256 views
1

我正在创建一个程序,该程序从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; 
+1

那么,如果你调试它会发生什么?是否有一些原因让你无法在代码的第一行中断,然后单步执行,运行到更多的断点或运行到光标,以找出问题出在哪里的索引?我们不知道您使用的是哪种测试数据。只需调试它! – 2012-04-24 23:59:37

+0

从你的问题我们只能告诉tempArray的数组维度,我的猜测是classesArray不够大。向我们展示如何声明和填充classesArray? – 2012-04-25 00:05:18

回答

1

您正在使用的整数(即w,x,y,z)正在变得比定义的数组大小更大。您应该在代码中添加断点,以便您可以在编译期间看到发生的情况,并查看它们变得比数组定义所期望的更大的位置。

您的循环的规则通常用于停止索引超出范围发生的异常。我会建议分解代码,似乎有很多事情正在进行,但对于这个循环似乎太多了。

3

通过调试代码步骤:

object[,] tempArray = new object[6,3]; 

您在tempArray[5, 2]创建具有最大索引的数组。然后你开始循环。在每个循环的开始:

int w = 0, x = 0; 

然后在循环体:

tempArray[w,x] = classesArray[y,z]; 

分配给tempArray[0, 0]

x += 2; 
z += 2; 
tempArray[w, x] = classesArray[y, z]; 

分配给tempArray[0, 2]

x++; 
z++; 
// Copies the hours EX: "3" from classesArray to tempArray 
tempArray[w, x] = classesArray[y, z]; 

您分配给tempArray[0, 3]。但是tempArray的最大索引是[0,2];你的数组索引超出范围,正是异常告诉你的。

如果你可以肯定的是yz永远不能去的classesArray边界之外,你可以声明tempArray这样的:

object[,] tempArray = new object[classesArray.GetLength(0), classesArray.GetLength(1)]; 

,但所有这些硬编码的幻数,并尝试同步具有不同基数的数组,这是非常危险的代码。

+1

,难以阅读。我无法理解为什么有评论解释'xyzzy'索引是什么,但索引名称只是连续的字母字符。如果'// w = row'没有调用变量'row'? – 2012-04-25 00:04:18

+0

@杜尔高拱 - 感谢您的帮助。我只是精神上和逻辑上耗尽了;我一直盯着12小时以上的相同代码。硬代码的原因是我只需要打印出数组的特定列;其余的只是用于后台错误检查,不会显示。我想出了基地不同的难题,如果我更好地了解这门语言,我会选择他们作为选择的基础。这是我在C#中的第一个项目,所以它可能不像熟练的程序员那样高效。 – Snuge 2012-04-25 00:22:02

+0

@ Martin James为了更好的可读性,我修正了PrintArray中的wxyz索引;我只是没有在这段代码中纠正它 – Snuge 2012-04-25 00:26:00