[英]Cast object[,] array element to int for conditional operator comparison c#
我试图将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
我的资料:
标头=呼叫,号码,班级名称,小时,先决条件编号,先决条件名称和标志。 第1行= MATH 2313、1000,微积分I,3、0、0和0。第2行= MATH 2113、1001,Calculus Lab I,1、0、0和0
我要打印第1行= MATH 2113,微积分I和3。第2行= MATH 2113,微积分实验室1和1
我用0填充了所有空元素
代码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++;
}
}
我肯定会更改整个代码,尤其是当它不是一次性任务时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.