473,854 Members | 1,543 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 29562
bool a = Convert.ToBoole an(i1);

Willy.

"Chris" <ch********@pan dora.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********@pan dora.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.ToBoole an instead of using (bool) i1

Convert.ToBoole an(i1);

It will work this way.

Regards,

Tarakeshwar

"Chris" <ch********@pan dora.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********@pan dora.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.r illing.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.co m>
http://www.pobox.com/~skeet
Nov 16 '05 #6
Doh!

Stupid me. :)

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Peter Rilling <pe***@nospam.r illing.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.co m>
http://www.pobox.com/~skeet

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

bool a = bool.Parse(i2.T oString());
"Chris" <ch********@pan dora.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*******@hotm ail.com> wrote:
I also enjoy doing something like:

bool a = bool.Parse(i2.T oString());

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

bool a = false;

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
Nov 16 '05 #9
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote:
Drebin <th*******@hotm ail.com> wrote:
I also enjoy doing something like:

bool a = bool.Parse(i2.T oString());

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.co m>
http://www.pobox.com/~skeet