1

Excel シートから情報の 2 次元オブジェクト配列をインポートするプログラムを作成しています。次に、この配列を 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

次の各行を個別にコメントアウトしましたが、コードの各行はすべて、上記と同じエラーを返します。

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;
4

2 に答える 2

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] です。あなたの配列インデックスは範囲外です。まさに例外があなたに伝えていたことです。

であり、 の境界の外に出ることはできないyと確信できる場合は、次のように宣言できます。zclassesArraytempArray

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

しかし、これらすべてのハードコーディングされたマジック ナンバーと、異なる基数を持つ配列を同期させようとすると、これは非常に危険なコードです。

于 2012-04-25T00:00:59.403 に答える
1

使用している整数 (つまり、w、x、y、z) が、定義された配列サイズよりも大きくなっています。コードにブレークポイントを追加して、コンパイル中に何が起こっているかを確認し、配列定義で予想されるよりも大きくなっている場所を確認する必要があります。

通常、ループのルールは、インデックスの境界外例外の発生を停止するために使用されます。コードを少し分割することをお勧めします。多くのことが行われているようですが、このループには多すぎるようです。

于 2012-04-24T23:59:22.157 に答える