将 object[,] 数组元素强制转换为 int 以进行条件运算符比较 c#

本文关键字:int 条件运算符 比较 转换 object 数组元素 | 更新日期: 2023-09-27 18:32:14

我正在尝试将 object[,] 数组元素转换为 int 以进行条件运算符比较。我的类数组由字符串和整数组成。 我引用的列只能具有 1 或 0 的值,因为我将其用作标志。如果有 1,我希望它继续到下一行/下一行。我使用的代码无法正确执行程序

while (classesArray[classesArrayRow,7] == (object)1)
                {
                    classesArrayRow++;
                }

我的整个 tempArray 填满了我的类数组的第一行。所以,我尝试尝试

while ((int)classesArray[classesArrayRow,7] > 0)

虽然没有错误,但此强制转换不起作用。

我的代码:

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 
        // if a break condition has not been met first.
        // It must reach 123 hours for CS Degree .
        int hourCounter = 0;
        int iteration = 0;
        while (hourCounter < 123)
        {
            // this while loop copies some classes from classes array to tempArray
            // so they can be printed into the excel template (NewStudentTemplateCS.xlsx)
            //
            int classes = 1, hours = 0; // stops while loop if limit is reached
            int tempArrayRow = 0, tempArrayCol = 0; // used to select individual elements of tempArray
            int classesArrayRow = 1, classesArrayCol = 1; // used to select individual elements of classesArray
            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[classesArrayRow,7] == (object)1)
                {
                    classesArrayRow++;
                }
                // copies the call EX: "MATH 2313" from classesArray to tempArray
                tempArray[tempArrayRow,tempArrayCol] = classesArray[classesArrayRow,classesArrayCol];
                tempArrayCol ++;
                classesArrayCol += 2;
                // copies the name EX: "Calculus I" from classesArray to tempArray
                tempArray[tempArrayRow, tempArrayCol] = classesArray[classesArrayRow, classesArrayCol];
                tempArrayCol++;
                classesArrayCol++;
                // Copies the hours EX: "3" from classesArray to tempArray
                tempArray[tempArrayRow, tempArrayCol] = classesArray[classesArrayRow, classesArrayCol];
                // increments classes, hours, and hourCounter for exit decision
                classes += 1;
                // converts object element to an int for the following "+=" operator
                int numberOfHours = Convert.ToInt32(classesArray[classesArrayRow, classesArrayCol]);
                // adds numberOfHours to the following varriable to increment loop exit decision
                hours += numberOfHours;
                hourCounter += numberOfHours;
                // sets flag to one
                classesArrayCol += 3;
                classesArray[classesArrayRow, classesArrayCol] = 1;
                //reset column varriables
                classesArrayCol = 1;
                tempArrayCol = 0;
                // increments row for temp array
                tempArrayRow++;
            }// 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

我的数据:

标头 = 呼叫、号码、类名、小时数、先决条件编号、先决条件名称和标志。第 1 行 = MATH 2313、1000、微积分 I、3、0、0 和 0。第 2 行 = MATH 2113、1001、微积分实验室 I、1、0、0 和 0

我要打印第 1 行 = MATH 2113、微积分 I 和 3。第 2 行 = MATH 2113,微积分实验室 1 和 1

我用 0 填充了所有空元素

将 object[,] 数组元素强制转换为 int 以进行条件运算符比较 c#

代码classesArray[classesArrayRow,7] == (object)1将执行引用比较,该比较应始终在盒装整数上返回 false。正确的方法是:

while ((int)classesArray[classesArrayRow,7] != 0){
    //...
}

解决问题的一种快速方法是编写代码来检查是字符串还是数字

if(classesArray[0,7] is int) // values were imported as ints
{
    while ((int)classesArray[classesArrayRow,7] == 1)
    {
        classesArrayRow++;
    }
}
if(classesArray[0,7] is string) // values were imported as strings
{
    while ((string)classesArray[classesArrayRow,7] == "0")
    {
        classesArrayRow++;
    }
}

我肯定会更改整个代码,尤其是当它不是一次性任务时。