Solved

How to check the value of an object

Posted on 2006-10-19
4
237 Views
Last Modified: 2010-04-16
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
Comment
Question by:aspnetdev
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
 
LVL 18

Expert Comment

by:Ravi Singh
Comment Utility
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
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
 
LVL 10

Expert Comment

by:NetworkArchitek
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now