473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 30086
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
14
by: Chris | last post by:
Hi, I try to print out truth-tables for an &&-operation using the following code, unfortunatly I get compiler errors : for ( byte i1=0; i1<=1; i1++) { for ( byte i2=0; i2<=1; i2++) { bool...
2
by: Jeff | last post by:
I get the following error: Cannot implicitly convert type 'Factory.Stack.pArrayStack' to 'Factory.Stack.StackDefs.ImStack' at compile time. I thought perhaps there was a mismatch between the...
2
by: Patrick Olurotimi Ige | last post by:
When i convert:- this code from VB to C# Why do i get error "Cannot implicitly convert type 'object' to 'bool' VB --- If cmdcommand.Parameters("ReturnValue").Value = 1 Then lblStatus.Text =...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
6
by: Doug | last post by:
Hi I have a short piece of trial code that compares some input and then produces a message based on the value. However I get a build error that i dont know how to resolve ' Cannot implicitly...
2
by: Christophe | last post by:
class A {} class B {} interface MyInterface { void method(A a); void method(B b); }
2
by: Nick | last post by:
I am trying to find out if particular array element contains InputOutput value. private static int FillParameters(DbCommand command, SqlParameter p) { int x = 0; //int initalize to hold zero...
4
by: hellomac23 | last post by:
Hi im trying to write a if...else for C# but i keep getting Cannot implicitly convert type 'int' to 'bool' i really lost can anybody help me on what that means
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.