473,804 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2307
On Oct 7, 12:27*pm, puzzlecracker <ironsel2...@gm ail.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.ToBoole an(i);
Oct 7 '08 #2
bool b = Convert.ToBoole an(i);
Thanks, that's what I was looking for!
Oct 7 '08 #3
puzzlecracker wrote:
>bool b = Convert.ToBoole an(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.co mwrote:
puzzlecracker wrote:
bool b = Convert.ToBoole an(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.co mwrote:
>puzzlecracke r wrote:
>>>bool b = Convert.ToBoole an(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.no spamwrote:
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
1094
by: djozy | last post by:
Please, how can I convert selected item from dropdown list to integer? Thank you
14
29533
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 a = (bool)i1; // ERROR : convert type 'byte' to 'bool'
9
31651
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 statement How do I convert the variable? regards
5
2069
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 "in/out" or "out" parameters ? Or is it simply that events are just notifications and are not interested in any values which event handlers might be able to return ? If the latter is the case, the event model in .NET appears to have a great...
37
11080
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 anyone that could tell me how to convert these two things? 1)
5
3796
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 List<RoleData> GetRoles() { return GetRoles(null, false); }
23
7425
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
0
6890
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 since this is the first time, i am working with databases and not really much of an expert. How can I rectify it? One thing more...... Should I be passing a value to type bit in sql server as 0 or '0'..... '0' is for character i guess ...
6
30202
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
10599
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9173
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7635
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.