Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.IndexOutOfRangeException-索引超出数组的边界_C#_Multidimensional Array_Outofrangeexception - Fatal编程技术网

C# System.IndexOutOfRangeException-索引超出数组的边界

C# System.IndexOutOfRangeException-索引超出数组的边界,c#,multidimensional-array,outofrangeexception,C#,Multidimensional Array,Outofrangeexception,我正在创建一个程序,从excel工作表导入二维对象数组信息。然后它将此数组传递给ProcessObjects方法进行处理并打印/导出回excel模板。有人能告诉我为什么会收到此错误消息吗 Project.exe中发生类型为“System.IndexOutOfRangeException”的未处理异常 附加信息:索引超出了数组的边界 private void ProcessObjects(object[,] classesArray, object[,] classesAvailabilityAr

我正在创建一个程序,从excel工作表导入二维对象数组信息。然后它将此数组传递给ProcessObjects方法进行处理并打印/导出回excel模板。有人能告诉我为什么会收到此错误消息吗

Project.exe中发生类型为“System.IndexOutOfRangeException”的未处理异常

附加信息:索引超出了数组的边界

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

您正在使用的整数,即w、x、y、z,正变得大于定义的数组大小。您应该向代码中添加断点,以便查看编译过程中发生的情况,并查看断点在何处变得比数组定义预期的大


循环的规则通常用于阻止索引越界异常的发生。我建议稍微分解一下代码,似乎有很多东西在进行,但对于这个循环来说似乎太多了。

您正在使用的整数,即w、x、y、z,正变得大于定义的数组大小。您应该向代码中添加断点,以便查看编译过程中发生的情况,并查看断点在何处变得比数组定义预期的大


循环的规则通常用于阻止索引越界异常的发生。我建议稍微分解一下代码,似乎有很多东西在进行,但对于这个循环来说似乎太多了。

在调试器中逐步检查代码:

object[,] tempArray = new object[6,3];
您正在创建一个最大索引为tempArray[5,2]的数组。然后你开始循环。在每个循环的开始处:

int w = 0, x = 0;
然后在循环体中:

tempArray[w,x] = classesArray[y,z];
您分配给tempArray[0,0]

您分配给tempArray[0,2]

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

如果您可以确定y和z永远不会超出classesArray的界限,那么可以这样声明tempArray:

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

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

在调试器中逐步检查代码:

object[,] tempArray = new object[6,3];
您正在创建一个最大索引为tempArray[5,2]的数组。然后你开始循环。在每个循环的开始处:

int w = 0, x = 0;
然后在循环体中:

tempArray[w,x] = classesArray[y,z];
您分配给tempArray[0,0]

您分配给tempArray[0,2]

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

如果您可以确定y和z永远不会超出classesArray的界限,那么可以这样声明tempArray:

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

但是,对于所有这些硬编码的幻数,并且试图用不同的基同步数组,这是一个非常危险的代码。

那么,如果调试它会发生什么?为什么不能在代码的第一行中断,然后单步执行、运行到更多断点或运行到游标以找出某个索引的问题所在?我们不知道您使用的是什么测试数据。调试一下!从您的问题中,我们只能知道tempArray的数组维度,我猜classesArray不够大。告诉我们如何声明和填充classesArray?如果调试它会发生什么?为什么不能在代码的第一行中断,然后单步执行、运行到更多断点或运行到游标以找出某个索引的问题所在?我们不知道您使用的是什么测试数据。调试一下!从您的问题中,我们只能知道tempArray的数组维度,我的猜测是classesArray不够大。向我们展示如何声明和填充classesArray?,而且很难阅读。我无法理解为什么会有注释解释“xyzzy”索引是什么,但索引名称只是连续的字母字符。如果“//w=row”有什么不可以调用变量“row”?@Dour High-Arch-谢谢您的帮助。我只是在精神上和逻辑上都筋疲力尽了;我已经盯着同一个代码看了12个多小时了。硬代码的原因是我只需要打印出数组的特定列;其余部分仅用于后台错误检查,不会显示。我好不容易才明白,这些基础是不同的;如果我更懂这门语言,我会选择让他们成为同样的基础。这是我在C语言中的第一个项目,所以它可能没有一个熟练的程序员那么高效。@Martin James我确实纠正了PrintArray中的wxyz索引以提高可读性;我只是没有在这段代码中更正它,而且很难阅读。我无法理解为什么会有注释解释“xyzzy”索引是什么,但索引名称只是连续的字母字符。如果“//w=row”有什么不可以调用变量“row”?@Dour High-Arch-谢谢您的帮助。我只是在思想上和逻辑上
筋疲力尽的我已经盯着同一个代码看了12个多小时了。硬代码的原因是我只需要打印出数组的特定列;其余部分仅用于后台错误检查,不会显示。我好不容易才明白,这些基础是不同的;如果我更懂这门语言,我会选择让他们成为同样的基础。这是我在C语言中的第一个项目,所以它可能没有一个熟练的程序员那么高效。@Martin James我确实纠正了PrintArray中的wxyz索引以提高可读性;我只是没有在代码中更正它