[Mono-list] Cast a char(1) to a boolean

Iain McCoy iain@mccoy.id.au
Sat, 23 Oct 2004 19:58:55 +1000


On Sat, 2004-10-23 at 07:22 +0200, Sijmen Mulder wrote:
> Hi,
> 
> > I was told to use a char(1) in a MySQL database since it doesn't have a
> > boolean type.  I retrieve a char(1) field but I can't seem to cast it to
> > be a boolean.  How is this done?
> 
> try:
> 
> bool thebool = (int)thechar==0 ? false : true;
> 
Ow, ow. It hurts.

Okay, I'm exaggerating. It's not really that bad, but that code is, as
far as I can see, needlessly complicated. All you need to do is this:
bool thebool = (thechar == 'T');
That's assuming you define an uppercase T to represent true, and
everything else to be a false. 

This might be a better definition:
bool thebool = (thechar != '\0');

Basically, just pick some character to be either true or false, and test
for that. Assign the result of a test to a boolean.

I'm fairly sure that that's the sanest approach to take.
-- 
Iain McCoy <iain@mccoy.id.au>