473,406 Members | 2,217 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.

convert int values to bool

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);
Oct 7 '08 #1
8 2283
On Oct 7, 12:27*pm, puzzlecracker <ironsel2...@gmail.comwrote:
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);
Oct 7 '08 #2
bool b = Convert.ToBoolean(i);
Thanks, that's what I was looking for!
Oct 7 '08 #3
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
Oct 7 '08 #4
On Oct 7, 5:39 pm, Göran Andersson <gu...@guffa.comwrote:
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?

Oct 8 '08 #5
puzzlecracker wrote:
On Oct 7, 5:39 pm, Göran Andersson <gu...@guffa.comwrote:
>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
Oct 8 '08 #6
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

Oct 9 '08 #7
On Oct 9, 11:06*am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
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
Oct 9 '08 #8
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


Oct 10 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: djozy | last post by:
Please, how can I convert selected item from dropdown list to integer? Thank you
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...
9
by: ReidarT | last post by:
I have a value from a field (int) in a table like string strPlayerLoginStatus = dsPlayer2.Tables.Rows.ToString(); I like to convert the strPlayerLoginStatus from string to int to use it in an if...
5
by: Edward Diener | last post by:
I am gathering from the documentation that return values from __events are not illegal but are frowned upon in .NET. If this is the case, does one pass back values from an event handler via...
37
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there...
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...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
0
by: weird0 | last post by:
Here is the code for executing stored procedure ExecuteSP() , and object that sets all its parameters in other functions and the stored procedure. I think it has something to do with db datatypes...
6
by: John | last post by:
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?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.