Solved

Issue with type casting in c#

Posted on 2014-12-28
4
120 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C#, asp.net MVC, sql server, LINQ 3 31
Video Player 11 22
SqlDependency to get update from sql to my c# app 2 36
C# Desktop Application 3 29
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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