Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Issue with type casting in c#

Posted on 2014-12-28
4
Medium Priority
?
142 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
[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
4 Comments
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 300 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 45

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:
Michael Fowler earned 1200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

610 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