Solved

Issue with type casting in c#

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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