469,284 Members | 2,443 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,284 developers. It's quick & easy.

Cannot convert type 'int' to 'bool'

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
6 27992
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
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
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
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
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
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.

Similar topics

14 posts views Thread by Chris | last post: by
2 posts views Thread by Jeff | last post: by
2 posts views Thread by Patrick Olurotimi Ige | last post: by
2 posts views Thread by Christophe | last post: by
2 posts views Thread by Nick | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.