Solved

How to check the value of an object

Posted on 2006-10-19
4
239 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
ID: 17767094
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
ID: 17767131
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
ID: 17767137
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
ID: 17767150
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 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