Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

How to check the value of an object

Hi,
i have the following code,now the object hasAttachments retruns me true or false .Depending on the value of hasAttachments i have to display icon can anyone tell me how do i check if the hasAttachments return true because this code is givin me error

 Cannot implicitly convert type 'object' to 'bool'


Object hasAttachments = e.Row.Cells.FromKey("HasAttachments").Value;

string attchStr;

if( hasAttachments = true  )
{
attchStr = "<a href=\"#\" onclick=\"window.open(\'Add.aspx?TPId=" + id + "&TPNumber=" + tpNum + "\'" + ", \'Attachment\','height=360,width=480'); return false;\"><img src=\'../Images/icon_attachment.jpg\' border=\'0\'></a>";
}
else
{
              attchStr = "<a href=\"#\" onclick=\"window.open(\'Add.aspx?TPId=" + id + "&TPNumber=" + tpNum + "\'" + ", \'Attachment\','height=360,width=480'); return false;\"><img src=\'../Images/addA.GIF\' border=\'0\'></a>";
}
0
aspnetdev
Asked:
aspnetdev
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Have you tried declaring hasAttachments as a bool?  Also you should be using == instead of =...

bool hasAttachments = e.Row.Cells.FromKey("HasAttachments").Value;

if (hasAttachments == true)
0
 
Ravi SinghSenior Software EngineerCommented:
Hi, you need to explicitly cast the object to a bool or convert the object:

bool hasAttachments = Convert.ToBoolean(e.Row.Cells.FromKey("HasAttachments").Value);

if (hasAttachments)
{
      attchStr = "<a href=\"#\" onclick=\"window.open(\'Add.aspx?TPId=" + id + "&TPNumber=" + tpNum + "\'" + ", \'Attachment\','height=360,width=480'); return false;\"><img src=\'../Images/icon_attachment.jpg\' border=\'0\'></a>";
}
else
{
     attchStr = "<a href=\"#\" onclick=\"window.open(\'Add.aspx?TPId=" + id + "&TPNumber=" + tpNum + "\'" + ", \'Attachment\','height=360,width=480'); return false;\"><img src=\'../Images/addA.GIF\' border=\'0\'></a>";
}
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You may need additional casting in there:

    bool hasAttachments = (bool)e.Row.Cells.FromKey("HasAttachments").Value;

But you could proably just do the whole thing in your "if" statement:

    if ((bool)e.Row.Cells.FromKey("HasAttachments").Value == true)
0
 
NetworkArchitekCommented:
This is an aside to your question but you may want to build your HTML with String.Format() instead of those concatenations. It makes life easier for me.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now