473,382 Members | 1,369 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,382 software developers and data experts.

convert int to bool

Often in dbs , tinyint is used instead of int (because tinyint cols may be
indexed , whereas int cols may not).

What is the best way to convert integers to booleans ?

The following fails:

bool myBool;
tinyint myTinyInt = 1;

myBool = (bool) myTinyInt;
If you have restricted your integers are restricted to 0 , 1, then the
following works, but is difficult to read:

myBool = (myTinyInt == 1);
Mar 3 '06 #1
4 12504
I don't know the best way, but I do know there is a Convert.ToBoolean
Method.

Here's a link to the MSDN ToBoolean Method:
http://msdn.microsoft.com/library/de...oleantopic.asp

Mar 3 '06 #2
Hello John A Grandy" johnagrandy-at-yahoo-dot-com,

Second one, (myTinyInt == 1)
Moreover, try to diminish this kind of bool variable and make (myTinyInt
== 1) in the place where do u need true/falce logic
it gives u more robust way.

J> Often in dbs , tinyint is used instead of int (because tinyint cols
J> may be indexed , whereas int cols may not).
J>
J> What is the best way to convert integers to booleans ?
J>
J> The following fails:
J>
J> bool myBool;
J> tinyint myTinyInt = 1;
J> myBool = (bool) myTinyInt;
J>
J> If you have restricted your integers are restricted to 0 , 1, then
J> the following works, but is difficult to read:
J>
J> myBool = (myTinyInt == 1);
J>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Mar 3 '06 #3

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
Often in dbs , tinyint is used instead of int (because tinyint cols may be
indexed , whereas int cols may not).
If you are using tinyint for bools then indexing is probably a bad idea.

What is the best way to convert integers to booleans ?

The following fails:

bool myBool;
tinyint myTinyInt = 1;

myBool = (bool) myTinyInt;
If you have restricted your integers are restricted to 0 , 1, then the
following works, but is difficult to read:

myBool = (myTinyInt == 1);


best be safe and use (myTinyInt != 0).

Consider doing the conversion in stored procedures or the query or an
expression column in the DataSet if you use them.

Mar 3 '06 #4
In C/C++ all integers different than 0 are considered to be true and all 0s
false. You can use this and convert like

bool myBool = (intValue != 0);

However it depends on your definitial of what is true and false for
integers.
--

Stoitcho Goutsev (100)

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
Often in dbs , tinyint is used instead of int (because tinyint cols may be
indexed , whereas int cols may not).

What is the best way to convert integers to booleans ?

The following fails:

bool myBool;
tinyint myTinyInt = 1;

myBool = (bool) myTinyInt;
If you have restricted your integers are restricted to 0 , 1, then the
following works, but is difficult to read:

myBool = (myTinyInt == 1);

Mar 3 '06 #5

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

Similar topics

3
by: klaas | last post by:
the following code gives rise to the beneath error message, only when a matrix object is instantiated as matrix<bool>, not with matrix<float>: /*returns a reference to the object at position...
14
by: JKop | last post by:
Will the following print "NOT equal" on all implementations: #include <iostream> signed main() { bool k; k = 5;
10
by: Nollie | last post by:
Does standard c++ define "true" to be always equal to 1? My code: int num = 1 + (value1== value2); Can I be certain that "num" will ALWAYS be either 1 or 2? How about on a Windows machine?
2
by: gh | last post by:
Hi every Body .. in C# if I wont the user to Input a Number I write the code: // ... the 1st way for int String Number; int X; Console.Write("Enter A Number:");
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...
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: 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?
32
by: Simon L | last post by:
BOOL bMyarray; memset(bMyArray , 0xFF, sizeof(BOOL) * 1000); clean & quick until someone else's code found I was returning -1 to mean true Because my BOOL is 4 bytes long, TRUE in memory...
8
by: puzzlecracker | last post by:
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);
4
by: cowdung7 | last post by:
public bool searchGame(string gameName) { string searchGame = {"Tibia", "Combat Arms", "Need for Speed", "Moto GP", "Risk", "Red Alert", "Monopoly", "Scrabble"}; if ( searchGame ==...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...

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.