Solved

Issue with type casting in c#

Posted on 2014-12-28
4
118 Views
Last Modified: 2015-01-20
Hi Experts,

I am getting following error when I try to retrieve value (1 or 0) from database.How to get rid of this error?

"operator == cannot be applied to operands of type bool and int"

//employeerepo.cs

pubic bool checkifempexist(int ecode)

{

//if employee exist in database  returns non-zero else returns zero

if(ecode==0)

 return false; //non-zero

else

return true;  //zero

}

Open in new window


Code to check if employee code exist or not

//employee.cs

bool isemployeeexists(employee e)

{

var rep=new employeerepo();

//retrieves 1 or 0 
var empcode=rep.checkifempexist(e.code)

if(empcode==1)  //Throwing error here
{
return true
}
return false;
}

Open in new window

0
Comment
Question by:ksd123
4 Comments
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 40520524
In this line of code,

var empcode = rep.checkifempexist(e.code)

the function checkifempexist is returning a bool type. In the following statement,

if( empcode == 1 )  //Throwing error here

you are comparing a bool, empcode, with the integer value of 1. They are not of the same data type. In C programming language this is fine but C# no.

either have checkifempexist return an integer value or change the 1 in the if statement to true.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40520556
instead of:

//retrieves 1 or 0
var empcode=rep.checkifempexist(e.code)

if(empcode==1)  //Throwing error here
{
return true
}
return false;


use this:
//retrieves 1 or 0
return (rep.checkifempexist(e.code) > 0);  true if the checkifempexists returns anything greater than zero
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 400 total points
ID: 40520958
The quickest way to resolve this would be to cast the response as an integer

int empcode = (int)rep.checkifempexist(e.code)

Open in new window

http://msdn.microsoft.com/en-US/library/ms173105(v=VS.100).aspx
or
int empcode= Convert.ToInt32(re.checkifempexist(e.code)

Open in new window

http://msdn.microsoft.com/en-us/library/2cew9dz7(v=vs.110).aspx

Alternatively you could have the method checkifempexist return an integer value

pubic int CheckIfEmpExist(int ecode)
{
   //if employee exist in database  returns non-zero else returns zero
   if (ecode==0)
      return 1; //non-zero
   else
      return 0;  //zero
}

Open in new window


Note: Using capitalisation can greatly assist in making your code easier to read
http://msdn.microsoft.com/en-us/library/vstudio/ms229043(v=vs.100).aspx
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40521718
In the isemployeeexists method, why not returning directly the result of the checkifempexist method?
bool isemployeeexists(employee e)
{
    var rep=new employeerepo(); 
    return rep.checkifempexist(e.code);
}

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

20 Experts available now in Live!

Get 1:1 Help Now