Thursday, April 27, 2006

Dealing with non-standard boolean values

I am have worked with how Access and other sources deliver non-standard variances of boolean variables.

After all when you update/insert to the database, all you want is a 1 or 0.

So I created this udf to help speed that process up.

<cfset one = "false">
<cfset two = "0">
<cfset new_one = 0>
<cfset new_two = 0>

<b>default values:</b><br>
<cfoutput>
one = #one#<br>
two = #two#<br>
</cfoutput>

<cfscript>
function booleanize(value) {
if (not isboolean(value)) {
value = replacenocase(value,'on',1);
value = replacenocase(value,'off',0);
}
if (yesnoformat(value) eq 'Yes') {
value = 1;
}
if (yesnoformat(value) eq 'No') {
value = 0;
}
return value;
}
</cfscript>


<cfoutput>
<b>New Values:</b><br>
one = #booleanize(one)#<br>
two = #booleanize(two)#<br>
</cfoutput>

This includes the udf as well as the test example.

I know it sounds simple, but this is easier than having to adjust your code logic for every checkbox, access on/off or true/false value.

Think about it.

3 comments:

  1. Anonymous3:32 PM

    Ahhh, you do know that CF is a typless languages right? So 1 and true and Yes are all treated the same. Same as 0, false and no can all be treated as false.

    ReplyDelete
  2. Anonymous8:51 AM

    Let's take the statement one step further. Any non-zero number is considered a true expression to include negative values.

    ReplyDelete
  3. Anonymous11:55 AM

    CF is typeless but Other things it may interface with are not.
    SQL does not take "yes" when you are updating a bit field.

    ReplyDelete