PC Review


Reply
Thread Tools Rate Thread

convert int values to bool

 
 
puzzlecracker
Guest
Posts: n/a
 
      7th Oct 2008
Is it possible, in a clean way, to convert int values too bool
int i=10;

bool b=(bool)i;


instead of doing b=(i>0);
 
Reply With Quote
 
 
 
 
Israel
Guest
Posts: n/a
 
      7th Oct 2008
On Oct 7, 12:27*pm, puzzlecracker <ironsel2...@gmail.com> wrote:
> Is it possible, in a clean way, to convert int values too bool
> int i=10;
>
> bool b=(bool)i;
>
> instead of doing b=(i>0);


I'd argue the statement should be:
bool b = (i != 0)

but this will work too:
bool b = Convert.ToBoolean(i);
 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      7th Oct 2008

> bool b = Convert.ToBoolean(i);


Thanks, that's what I was looking for!
 
Reply With Quote
 
Göran Andersson
Guest
Posts: n/a
 
      7th Oct 2008
puzzlecracker wrote:
>> bool b = Convert.ToBoolean(i);

>
> Thanks, that's what I was looking for!


And guess how it's implemented?

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



--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      8th Oct 2008
On Oct 7, 5:39 pm, Göran Andersson <gu...@guffa.com> wrote:
> puzzlecracker wrote:
> >> bool b = Convert.ToBoolean(i);

>
> > Thanks, that's what I was looking for!

>
> And guess how it's implemented?
>
> public static bool ToBoolean(int value) {
> return (value != 0);
>
> }
>
>
>
> --

Göran Andersson
> _____http://www.guffa.com


Sweet, what about the reverse?

 
Reply With Quote
 
Arne Vajhřj
Guest
Posts: n/a
 
      8th Oct 2008
puzzlecracker wrote:
> On Oct 7, 5:39 pm, Göran Andersson <gu...@guffa.com> wrote:
>> puzzlecracker wrote:
>>>> bool b = Convert.ToBoolean(i);
>>> Thanks, that's what I was looking for!

>> And guess how it's implemented?
>>
>> public static bool ToBoolean(int value) {
>> return (value != 0);
>>
>> }

>
> Sweet, what about the reverse?


You mean:

public static int ToInt32(bool value)
{
if (!value)
{
return 0;
}
return 1;
}

?

I don't know why it is not using the ?: operator.

Arne
 
Reply With Quote
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      9th Oct 2008
> You mean:
>
> public static int ToInt32(bool value)
> {
> if (!value)
> {
> return 0;
> }
> return 1;
> }
>
> ?
>
> I don't know why it is not using the ?: operator.


Did you look at the source code or use a decompiler? The MSIL doesn't
provide any distinction that a decompiler could use between an if/else vs
tertiary operator.

>
> Arne



 
Reply With Quote
 
Duggi
Guest
Posts: n/a
 
      9th Oct 2008
On Oct 9, 11:06*am, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote:
> > You mean:

>
> > public static int ToInt32(bool value)
> > {
> > * * if (!value)
> > * * {
> > * * * * return 0;
> > * * }
> > * * return 1;
> > }

>
> > ?

>
> > I don't know why it is not using the ?: operator.

>
> Did you look at the source code or use a decompiler? *The MSIL doesn't
> provide any distinction that a decompiler could use between an if/else vs
> tertiary operator.
>
>
>
>
>
> > Arne- Hide quoted text -

>
> - Show quoted text -


right.

it basically converts to one and the same

-Cnu
 
Reply With Quote
 
Arne Vajhřj
Guest
Posts: n/a
 
      10th Oct 2008
Ben Voigt [C++ MVP] wrote:
>> You mean:
>>
>> public static int ToInt32(bool value)
>> {
>> if (!value)
>> {
>> return 0;
>> }
>> return 1;
>> }
>>
>> ?
>>
>> I don't know why it is not using the ?: operator.

>
> Did you look at the source code or use a decompiler? The MSIL doesn't
> provide any distinction that a decompiler could use between an if/else vs
> tertiary operator.


Reflector.

So it could actually have been a ?: operator ?

Well - easy to test.

public static class ToBeOrNotToBe
{
public static int Foo(bool v)
{
return v ? 1 : 0;
}
public static int Bar(bool v)
{
if(v)
{
return 1;
}
else
{
return 0;
}
}
}

csc and reflector:

public static class ToBeOrNotToBe
{
// Methods
public static int Bar(bool v)
{
if (v)
{
return 1;
}
return 0;
}

public static int Foo(bool v)
{
return (v ? 1 : 0);
}
}

csc /o+ and reflector:

public static class ToBeOrNotToBe
{
// Methods
public static int Bar(bool v)
{
if (v)
{
return 1;
}
return 0;
}

public static int Foo(bool v)
{
if (!v)
{
return 0;
}
return 1;
}
}

Yes - it looks as if the ?: has been used after all.

Arne




 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot convert type 'int' to 'bool' John Microsoft C# .NET 9 1st Feb 2011 05:27 PM
cannot implicitly convert to bool =?Utf-8?B?Tmljaw==?= Microsoft C# .NET 2 3rd Nov 2006 08:18 PM
convert int to bool John A Grandy Microsoft C# .NET 4 3rd Mar 2006 03:02 PM
Convert a string to bool ReidarT Microsoft C# .NET 9 20th Feb 2005 10:10 AM
Cannot convert to bool error in VB but not C# Bill Dee Microsoft VB .NET 12 5th May 2004 02:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:44 PM.