Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 7 Operator '==' cannot be applied to operands of type 'object' and 'bool'

Tags:

c#

asp.net

I'm converting a lot of code from VB.net to c# and here is another issue I think cropped up during the conversion.

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

on each of the if / else / else if lines it gives me this error. I'm sure I am missing something that will force me to do a head bonk...

Error 7 Operator '==' cannot be applied to operands of type 'object' and 'bool'

like image 652
James Wilson Avatar asked Aug 14 '12 22:08

James Wilson


1 Answers

Are you sure that these values are of type bool?

If so, just explicitly cast:

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";
}
like image 106
Andrew Shepherd Avatar answered Oct 08 '22 00:10

Andrew Shepherd