Search code examples
c#excel-interopassignment-operatorlistobject

Checking ListObject value


I am trying to check a range within my ListObject to see if it contains the number 9. I keep getting a "Cannot implicitly convert type 'int' to 'bool'".

if (xlWorkBook.Worksheets["Sheet1"].ListObjects["table1"].Range[1, 1].value2 = 9)
{
    xlSheet.Cells[5, 5] = "YES!";
}
else 
{ 
    xlSheet.Cells[5, 5] = "NO!"; 
}

Resolved answer:

if (xlWorkBook.Worksheets["Sheet1"].ListObjects["table1"].Range[1, 1].value2 == 9)
{
    xlSheet.Cells[5, 5] = "YES!";
}
else 
{ 
    xlSheet.Cells[5, 5] = "NO!"; 
}

After reading up on operators for C#, I found out that "==" is used for comparison, while "=" is used for assignment.


Solution

  • if (xlWorkBook.Worksheets["Sheet1"].ListObjects["table1"].Range[1, 1].value2 == 9)
    

    Your if should look like this. Make a difference between = and ==