469,323 Members | 1,516 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

convert type 'byte' to 'bool'

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'
bool b = (bool)i2; // ERROR : convert type 'byte' to 'bool'
bool result = a && b;
}
}

how do I solve this ?
thnx
Chris
Nov 16 '05 #1
14 27372
bool a = Convert.ToBoolean(i1);

Willy.

"Chris" <ch********@pandora.be> wrote in message
news:tv***********************@phobos.telenet-ops.be...
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'
bool b = (bool)i2; // ERROR : convert type 'byte' to 'bool'
bool result = a && b;
}
}

how do I solve this ?
thnx
Chris

Nov 16 '05 #2
Why not just do "bool result = i1 && i2".

"Chris" <ch********@pandora.be> wrote in message
news:tv***********************@phobos.telenet-ops.be...
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'
bool b = (bool)i2; // ERROR : convert type 'byte' to 'bool'
bool result = a && b;
}
}

how do I solve this ?
thnx
Chris

Nov 16 '05 #3
use Convert.ToBoolean instead of using (bool) i1

Convert.ToBoolean(i1);

It will work this way.

Regards,

Tarakeshwar

"Chris" <ch********@pandora.be> wrote in message
news:tv***********************@phobos.telenet-ops.be...
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'
bool b = (bool)i2; // ERROR : convert type 'byte' to 'bool'
bool result = a && b;
}
}

how do I solve this ?
thnx
Chris

Nov 16 '05 #4
Hi,

Assuming that you consider a value different to 0 true ( the default ) ,
this will do:

bool b = byte >0;
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Chris" <ch********@pandora.be> wrote in message
news:tv***********************@phobos.telenet-ops.be...
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'
bool b = (bool)i2; // ERROR : convert type 'byte' to 'bool'
bool result = a && b;
}
}

how do I solve this ?
thnx
Chris

Nov 16 '05 #5
Peter Rilling <pe***@nospam.rilling.net> wrote:
Why not just do "bool result = i1 && i2".


Because there's no operator && which has byte on each side of it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Doh!

Stupid me. :)

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Peter Rilling <pe***@nospam.rilling.net> wrote:
Why not just do "bool result = i1 && i2".


Because there's no operator && which has byte on each side of it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7
I also enjoy doing something like:

bool a = bool.Parse(i2.ToString());
"Chris" <ch********@pandora.be> wrote in message
news:tv***********************@phobos.telenet-ops.be...
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'
bool b = (bool)i2; // ERROR : convert type 'byte' to 'bool'
bool result = a && b;
}
}

how do I solve this ?
thnx
Chris

Nov 16 '05 #8
Drebin <th*******@hotmail.com> wrote:
I also enjoy doing something like:

bool a = bool.Parse(i2.ToString());


You may enjoy it, but it's just an expensive way of doing

bool a = false;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Drebin <th*******@hotmail.com> wrote:
I also enjoy doing something like:

bool a = bool.Parse(i2.ToString());


You may enjoy it, but it's just an expensive way of doing

bool a = false;


Apologies - it's actually an expensive way of throwing a new
FormatException. Still doesn't do anything useful though ;)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
The original post was about casting a byte as a bool... it sounds like you
jumped in mid-stream here!? :-)
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Drebin <th*******@hotmail.com> wrote:
> I also enjoy doing something like:
>
> bool a = bool.Parse(i2.ToString());


You may enjoy it, but it's just an expensive way of doing

bool a = false;


Apologies - it's actually an expensive way of throwing a new
FormatException. Still doesn't do anything useful though ;)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #11
Drebin <th*******@hotmail.com> wrote:
The original post was about casting a byte as a bool... it sounds like you
jumped in mid-stream here!? :-)


No, I understood what the OP wanted. I just can't see how your code
helps. Were you meaning it as a joke?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #12
Hi,

Are you converting a byte to string, then the string back to bool?

What is the point of that?

the easiest and I believe fastest way of doing this is
bool b= byte_var >0;

why using Convert.XXX methods or any other method, for something that can be
solved with a simple operation?
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Drebin" <th*******@hotmail.com> wrote in message
news:L3*******************@newssvr19.news.prodigy. com...
I also enjoy doing something like:

bool a = bool.Parse(i2.ToString());
"Chris" <ch********@pandora.be> wrote in message
news:tv***********************@phobos.telenet-ops.be...
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' bool b = (bool)i2; // ERROR : convert type 'byte' to 'bool' bool result = a && b;
}
}

how do I solve this ?
thnx
Chris


Nov 16 '05 #13
lol No John, my post was not a joke... yeah, I wrote that off the top of my
head.. .sometimes my posts help, sometimes they end up being useless. :-)
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Drebin <th*******@hotmail.com> wrote:
The original post was about casting a byte as a bool... it sounds like you jumped in mid-stream here!? :-)


No, I understood what the OP wanted. I just can't see how your code
helps. Were you meaning it as a joke?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #14
Nevermind me, that doesn't work anyhow.. see? Don't be like me - think
outside of the box!! Think simple!!

I've been doing long.Parse() all week.. "Ask a carpenter to solve a problem,
he'll give you a hammer" :-)
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:OF*************@TK2MSFTNGP10.phx.gbl...
Hi,

Are you converting a byte to string, then the string back to bool?

What is the point of that?

the easiest and I believe fastest way of doing this is
bool b= byte_var >0;

why using Convert.XXX methods or any other method, for something that can be solved with a simple operation?
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Drebin" <th*******@hotmail.com> wrote in message
news:L3*******************@newssvr19.news.prodigy. com...
I also enjoy doing something like:

bool a = bool.Parse(i2.ToString());
"Chris" <ch********@pandora.be> wrote in message
news:tv***********************@phobos.telenet-ops.be...
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' bool b = (bool)i2; // ERROR : convert type 'byte' to 'bool' bool result = a && b;
}
}

how do I solve this ?
thnx
Chris



Nov 16 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

24 posts views Thread by djozy | last post: by
2 posts views Thread by jiangyh | last post: by
2 posts views Thread by Anthony Borla | last post: by
4 posts views Thread by msosno01 | last post: by
13 posts views Thread by Bob | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.