By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,703 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,703 IT Pros & Developers. It's quick & easy.

Cannot convert type 'int' to 'bool'

P: n/a
The following code:

int test = 1;
bool isTrue = (bool)test;

results in a compiler error:

Cannot convert type 'int' to 'bool'

wtf, any ideas on how to work around this?
Sep 22 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I don't know why you get this error but it could be related with bool values
being limited to 1 or 0. In this case, the int is 1, but I wouldn't want the
compiler trying to determine stuff like that--too much overhead.

How about something like this:

bool isTrue = (test != 0);

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
"John" <no@spam.comwrote in message
news:u9**************@TK2MSFTNGP02.phx.gbl...
The following code:

int test = 1;
bool isTrue = (bool)test;

results in a compiler error:

Cannot convert type 'int' to 'bool'

wtf, any ideas on how to work around this?
Sep 22 '07 #2

P: n/a
Jonathan Wood wrote:
How about something like this:

bool isTrue = (test != 0);
Yes, it seems that their is no implicit conversion of int to bool,
therefore the only recourse is to specifically use an equivalence
operator. Oh well, must have something to do with the overhead of the
JIT. Well, not that the JIT implicitly has overhead, it's just that we
have to specifically spoon feed it exactly we want.
Sep 22 '07 #3

P: n/a
On Fri, 21 Sep 2007 21:39:26 -0700, John <no@spam.comwrote:
>The following code:

int test = 1;
bool isTrue = (bool)test;

results in a compiler error:

Cannot convert type 'int' to 'bool'

wtf, any ideas on how to work around this?
bool isTrue = Convert.ToBoolean(test);

You will find that non-zero integer arguments to Convert.ToBoolean all
return true.

regards
A.G.
Sep 22 '07 #4

P: n/a
check it its working
int k= 1;
bool b = Convert.ToBoolean(k);

"John" wrote:
The following code:

int test = 1;
bool isTrue = (bool)test;

results in a compiler error:

Cannot convert type 'int' to 'bool'

wtf, any ideas on how to work around this?
Sep 22 '07 #5

P: n/a
John <no@spam.comwrote:
bool isTrue = (test != 0);
Yes, it seems that their is no implicit conversion of int to bool,
therefore the only recourse is to specifically use an equivalence
operator. Oh well, must have something to do with the overhead of the
JIT. Well, not that the JIT implicitly has overhead, it's just that we
have to specifically spoon feed it exactly we want.
No, it's got nothing to do with JIT overhead - it's to do with program
correctness. An implicit conversion from int to bool could easily lead
to unintended consequences, such as:

int x;
if (x=3)
{
....
}

Conceptually an integer isn't true or false. Just because C and C++
have traditionally treated any non-zero value as "true" doesn't mean
it's a good idea.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 22 '07 #6

P: n/a
Registered User wrote:
On Fri, 21 Sep 2007 21:39:26 -0700, John <no@spam.comwrote:
>The following code:

int test = 1;
bool isTrue = (bool)test;

results in a compiler error:

Cannot convert type 'int' to 'bool'

wtf, any ideas on how to work around this?

bool isTrue = Convert.ToBoolean(test);

You will find that non-zero integer arguments to Convert.ToBoolean all
return true.

regards
A.G.
The Convert.ToBoolean(int) method is implemented as:

public static bool ToBoolean(int value) {
return (value != 0);
}

So it's the same as doing the operation yourself:

bool isTrue = (test != 0);

Perhaps the compiler manages to inline the method call, so that the
generated code is the same in both cases. Either way the difference in
performance is normally negligable, so you should just go with the one
that best describes why you are doing the conversion.

:)

--
Göran Andersson
_____
http://www.guffa.com
Sep 23 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.