convert type 'byte' to 'bool'

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
 
T

Tarakeshwar L

use Convert.ToBoolean instead of using (bool) i1

Convert.ToBoolean(i1);

It will work this way.

Regards,

Tarakeshwar
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

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

bool b = byte >0;


cheers,
 
J

Jon Skeet [C# MVP]

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

Jon Skeet [C# MVP]

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 ;)
 
D

Drebin

The original post was about casting a byte as a bool... it sounds like you
jumped in mid-stream here!? :)
 
J

Jon Skeet [C# MVP]

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

Ignacio Machin \( .NET/ C# MVP \)

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

Drebin

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. :)
 
D

Drebin

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" :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top