472,127 Members | 2,008 Online

# 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 28493
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
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

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
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
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

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
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

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.