convert type 'byte' to 'bool'

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

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
 
use Convert.ToBoolean instead of using (bool) i1

Convert.ToBoolean(i1);

It will work this way.

Regards,

Tarakeshwar
 
Hi,

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

bool b = byte >0;


cheers,
 
Drebin said:
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 said:
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 ;)
 
The original post was about casting a byte as a bool... it sounds like you
jumped in mid-stream here!? :-)
 
Drebin said:
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?
 
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,
 
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. :-)
 
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" :-)
 
Back
Top