Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to check the value of an object

Posted on 2006-10-19
4
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 1500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

671 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