Sometimes it's the little things

12 November 2007

It's not often I'm stumped with an error in ColdFusion, but this one had me for a while.

I've been doing this ColdFusion thing for, as of today, a month short of 7 years. I'm not often suprised by CF but this had me thinking long and hard for embarrassingly longer than I would like to admit.

Now, without looking too far down the page, what would you expect from the following:

<cfset VARIABLES.someVar = "">    <cfif someConditionThatMightBeTrue>     <cfset VARIABLES.someVar = CreateObject("component", "path.to.my.cfc")>  </cfif>    <cfif VARIABLES.someVar EQ "">   We haven't created the object yet.  <cfelse>   We have an object!  </cfif>

Me? I was thinking it would work as I wanted. Check to see if VARIABLES.someVar was an empty string, if not, the object hadn't been created.

Why then did it take me so long to see that comparing a string to an object, once it had been created, with EQ was the reason things weren't working as I expected?

You'd think the following error would have speeded up the debugging process also!

Complex object types cannot be converted to simple values.

Anyway, the point is. Use IsSimpleValue() rather than EQ.