Cannot convert type 'int' to 'bool'

J

John

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?
 
J

Jonathan Wood

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);
 
J

John

Jonathan said:
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.
 
R

Registered User

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.
 
J

Jon Skeet [C# MVP]

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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Registered said:
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.

:)
 
R

Ronald Boucher

How about this?

bool isTrue = (test == 1 ? true : false);
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?
On Saturday, September 22, 2007 12:48 AM Jonathan Wood wrote:
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


news:[email protected]...
On Saturday, September 22, 2007 3:08 AM Jon Skeet [C# MVP] wrote:


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.
 
J

Jeff Johnson

How about this?

bool isTrue = (test == 1 ? true : false);

Eww. Let's talk about what's icky about that solution. First, let's look at
the ternary operator. You're testing a condition, which will result in true
of false, and then based on that true or false, you're going to
return...true or false! Why not just return the result of the condition?

Next, while your answer solves the stated problem in an exactly-as-worded
manner, but it should be obvious to any seasoned programmer that the OP
wants zero = false, non-zero = true. So the better test would be

bool isTrue = test != 0;

Oh, and congrats on answering a three-and-a-half-year-old post. I wonder if
the OP has been sitting at his machine all this time hitting F5....
 
J

Jeff Johnson

[...]
Oh, and congrats on answering a three-and-a-half-year-old post. I wonder
if
the OP has been sitting at his machine all this time hitting F5....

I really doubt that there is in fact a "Ronald Boucher", and also doubt as
well that whoever the person who posted that is will be coming back to
read the replies.

I wouldn't have replied if I had seen the standard EggHead cafe footer, but
that one didn't have it (except quoted).
These "late follow-up" posts are just too bizarre and yet
regularly-appearing to be genuine. Someone's up to some funny business,
and it has practically nothing to do with C#, whatever it is.

You think? I've always thought it was n00Bs who just got into the whole
forum scene and hadn't received a proper smackdown concerning necroposting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top