【问题标题】:Cast object[,] array element to int for conditional operator comparison c#将 object[,] 数组元素转换为 int 以进行条件运算符比较 c#
【发布时间】:2012-04-25 04:29:24
【问题描述】:

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

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

我的整个 tempArray 都被我的 classesArray 的第一行填满了。所以,我试图尝试

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

我的数据:

header = 电话、号码、班级名称、小时数、先决条件编号、先决条件名称和标志。 第 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 填充所有空元素

【问题讨论】:

  • classesArraytempArrayobject[,] 类型而不是 int[,] 类型的任何原因?在您提供的代码中,我看不出您为什么要使用这种object 而不是更具体的类型。
  • 你说演员阵容没有错误,但不起作用。您能否更具体地说明非工作部分?
  • @Albin Sunnanbo - 消息框显示“Project.exe 中发生了 'System.InvalidCastException' 类型的未处理异常附加信息:指定的转换无效。”
  • @siride 这是我的第一个 C# 项目,我不熟悉所有的命令、数据类型等;所以这可能不是最有效的编程方式。我只是尽力让它发挥作用。
  • 能解释一下你用这个方法做什么以及classesArray里面有什么吗?

标签: c# multidimensional-array while-loop int type-conversion


【解决方案1】:

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

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

【讨论】:

  • 我刚试了一下,收到了同样的错误“Project.exe 中发生了'System.InvalidCastException' 类型的未处理异常附加信息:指定的转换无效。”
  • @Snuge:看起来 classesArray[,] 包含不正确的数据。
  • 他说“我的 classesArray 由字符串和整数组成。”您正在尝试将字符串转换为 int
  • @lucas:他还说“我引用的列的值只能是 1 或 0,因为我将它用作标志”
  • 其中一个是假的 :) @Snuge 你为什么用 classesArrayRow 索引表格 1?
【解决方案2】:

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

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++;
    }
}

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

【讨论】:

  • 第二个 if 语句部分有效。唯一的问题是 tempArray 的所有行都被 classesArray 的第一个 live 填充。
猜你喜欢
  • 2018-10-04
  • 2012-02-27
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
  • 2016-01-11
  • 1970-01-01
  • 2015-02-24
  • 2012-05-13
相关资源
最近更新 更多