Solved

How to check the value of an object

Posted on 2006-10-19
4
238 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

932 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

10 Experts available now in Live!

Get 1:1 Help Now