Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 4th, 2005, 02:34 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cannot implicitly convert type int to bool

Hi Friends,
i would like your help as i am getting some problems with the following code.
i have marked the lines in red color.
please help me out.

private void btnauthor_Click(object sender, System.EventArgs e)
{
    int i;
    CheckBox cb;
    DataGridItem dgitem;
    for(i=0;dgemp.Items.Count-1;i++)
    {
         dgitem= dgemp.Items[i];
         cb=(CheckBox)(dgitem.FindControl("chk1"));
    }
    if(cb ="" && cb.Checked )
    {
        ordList += dgitem.Cells[1].Text + "";
    }
    lblexec.Text= KillEnding(ordList.Trim());
}

private string KillEnding(string sItem)
{
    return sItem.Substring(0,sItem.Length-1);
}



 
Old May 4th, 2005, 04:32 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

You are assigning cb="" and checking.That is the problem.Check with == operator instead of =

 
Old May 4th, 2005, 05:13 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks...
but i need to know about the first problem also....ie for loop one...

Regards,
muskaan

 
Old May 4th, 2005, 05:23 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

Hi u didnt mention the condition for it
for(i=0;i > dgemp.Items.Count-1;i++)


 
Old May 4th, 2005, 05:40 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
i am still getting error for...
  if(cb ="" && cb.Checked )
and the error is : operator == cannot be applied to the operands of the type "system.web.ui.webcontrols.checkbox" and "string"

Thanks & Regards,
Muskaan.

 
Old May 4th, 2005, 05:47 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

cb is an object..Check for null and cb.Checked

 
Old May 4th, 2005, 06:18 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
please let me tell you the details.

I am trying to display checkboxes in the datagrid.

In this program, I have done the coding for “Select All” and “Clear All” buttons but I am getting error for “GetEmployeeIDs” button.



The code is as follows:



private void btnauthor_Click(object sender, System.EventArgs e)

            {

                  int i;

                  CheckBox cb;

                  DataGridItem dgitem;

                  for(i=0;dgemp.Items.Count-1;i++)
                  {

                         dgitem= dgemp.Items[i];

                         cb=(CheckBox)(dgitem.FindControl("chk1"));

                  }

                  if(cb ==null && cb.Checked )
                  {

                        ordList += dgitem.Cells[1].Text + "";

                  }

                  lblexec.Text= KillEnding(ordList.Trim());

            }



            private string KillEnding(string sItem)

            {

                  return sItem.Substring(0,sItem.Length-1);

            }

 now, i am getting runtime error and that is : object reference not set to an instance of the object.

Thanks & Regards,

Muskaan.





 
Old May 4th, 2005, 06:48 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You might run into problems with this

if(cb ==null && cb.Checked )
{
...
}

If cb is null, then the if will fail but the second test of the if will crash becuase cb is null.

Try this:

if(cb==null)
{
    if(cb.Checked)
    {
    ...
    }
}

-Peter
 
Old May 4th, 2005, 06:50 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Wait, sorry, that makes no sense. Why would you test for cb==null AND see if it's checked??

-Peter
 
Old May 4th, 2005, 03:53 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

peter,this doesn't crash if cb is null because of compiler optimization,
if(cb ==null || cb.Checked ){}

_____________
Mehdi.
software student.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot implicitly convert type ... lcarpay ASP.NET 2.0 Basics 3 June 22nd, 2007 11:20 PM
cant implicitly convert type int to bool muskaanbajaj BOOK: ASP.NET Website Programming Problem-Design-Solution 3 November 19th, 2005 04:57 PM
How to Convert a int in a bool with FormatStrings ShadowMoon Classic ASP Basics 2 October 26th, 2004 05:53 AM
Can't convert type 'bool' to 'int' Gjerstad BOOK: ASP.NET Website Programming Problem-Design-Solution 2 July 9th, 2003 10:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.