我正在将很多代码从VB.net转换为c#,这是我认为在转换过程中出现的另一个问题.
if (sRow.Cells[1].Value == true)
Worked = "X";
else if (sRow.Cells[2].Value == true)
Vacation = "X";
else if (sRow.Cells[3].Value == true)
Sick = "X";
else if (sRow.Cells[4].Value == true)
Holiday = "X";
Run Code Online (Sandbox Code Playgroud)
在每个if/else/else if行上它给我这个错误.我确定我错过了一些会迫使我做头脑的东西......
错误7运算符'=='无法应用于'object'类型和'bool'类型的操作数
And*_*erd 10
你确定这些值是bool什么类型的吗?
如果是这样,只需明确表达:
if ((bool)sRow.Cells[1].Value)
{
Worked = "X";
}
else if ((bool)sRow.Cells[2].Value)
{
Vacation = "X";
}
else if (sRow.Cells[3].Value)
{
Sick = "X";
}
else if ((bool)sRow.Cells[4].Value)
{
Holiday = "X";
}
Run Code Online (Sandbox Code Playgroud)
我假设这是一个DataRow的单元格,其值是类型object.您无法将具有bool的对象与==运算符进行比较.
所以你应该使用强类型Field扩展DataRow:
if(sRow.Field<bool>(1))
// ...
Run Code Online (Sandbox Code Playgroud)