Solved

Issue with type casting in c#

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

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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 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