[Mono-list] Cast a char(1) to a boolean
Eric Damron
edamron@spamcop.net
Sat, 23 Oct 2004 12:16:23 -0700
Iain McCoy wrote:
>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.
>
>
Thanks for your replys. I'm still having a problem. Here is a code
snippit:
ds = dbManager.GetState(ref conn);
dt = ds.Tables[0];
foreach (DataRow dr in dt.Rows)
{
if ((char)dr[ "running"] == 'T')
{
this.ProcessRecords(dr);
}
}
ds is a DataSet, dt is a DataTable and the value is being returned in an
object of type DataRow. If I run it I get the runtime error:
Unhandled Exception: System.InvalidCastException: Cannot cast from
source type to destination type.
Thanks