Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Cannot convert type 'int' to 'bool'

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John - 22 Sep 2007 05:39 GMT
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?
Jonathan Wood - 22 Sep 2007 05:48 GMT
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);

Signature

Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

> The following code:
>
[quoted text clipped - 6 lines]
>
> wtf, any ideas on how to work around this?
John - 22 Sep 2007 07:20 GMT
 > 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.
Jon Skeet [C# MVP] - 22 Sep 2007 08:08 GMT
> > 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.

Signature

Jon Skeet - <skeet@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

Registered User - 22 Sep 2007 07:25 GMT
>The following code:
>
[quoted text clipped - 6 lines]
>
>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.
Göran Andersson - 23 Sep 2007 22:49 GMT
>> The following code:
>>
[quoted text clipped - 14 lines]
> 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.

:)

Signature

Göran Andersson
_____
http://www.guffa.com

Som Nath Shukla - 22 Sep 2007 08:00 GMT
check it its working  
int k= 1;
 bool b = Convert.ToBoolean(k);

> The following code:
>
[quoted text clipped - 6 lines]
>
> wtf, any ideas on how to work around this?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.