Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at the jQuery Conference 2009 (Cambridge, MA) with:

Destroying ColdFusion Variables Using JavaCast()

By Ben Nadel on
Tags: ColdFusion

Not really that interesting, but you can destroy variables by setting them equal to the result of a JavaCast() to "null". I have beeing some testing with null values in ColdFusion lately and I did this for a goof:

  • <!--- Create the objFoo variable. --->
  • <cfset objFoo = StructNew() />
  • <cfset objFoo.Bar = "Well Played!" />
  •  
  • <!--- Destroy struct via JavaCast(). --->
  • <cfset objFoo = JavaCast( "null", 0 ) />
  •  
  • <!--- Dump out variable. --->
  • <cfdump var="#objFoo#" />

This throws the error:

Variable OBJFOO is undefined.

Now, I am not recommending this, nor should this ever be a replacement for the ColdFusion function StructDelete(). In fact, if you try to use this to destroy a structure element, it doesn't "really" work. Let's rework the code to destroy the structure key, not the structure:

  • <!--- Create the objFoo variable. --->
  • <cfset objFoo = StructNew() />
  • <cfset objFoo.Bar = "Well Played!" />
  •  
  • <!--- Destroy key via JavaCast(). --->
  • <cfset objFoo.Bar = JavaCast( "null", 0 ) />
  •  
  • <!--- Dump out variable. --->
  • <cfdump var="#objFoo#" />

This time, when you dump out the structure, "Bar" is still a key in the structure:


 
 
 

 
undefined struct element  
 
 
 

However, its value is "[undefined struct element]" and any attempt to reference it directly will throw the error:

Element BAR is undefined in OBJFOO.

Interesting stuff.




Reader Comments

LOL "not really interesting"? This is extremely interesting if you use isdefined() to test for the existence of a single session variable and you want to destroy that one var without clearing the whole session scope!

Reply to this Comment

This helped hugely. @cfjedimaster pointed me in the Javacast direction, then i found this from @bennadel.

I have a QoQ running after drawing out the data from an XML file.

My empty variables of "" were not working and throwing a Null Pointer error with no other information and hard to debug.

This is how i fixed it:

<cfset temp = QuerySetCell(myquery, "listing_field1", JavaCast( "null", 0 ), #i#)>

Sweet!

Reply to this Comment

@Brien,

You can always use structDelete() to clear variables as well (which might be a little more self-documenting).

@Leigh,

Very nice! Query of Queries is awesome; but, they can really throw you through a loop when dealing with type-casting. I've learned to always always always use javaCast() when manually setting query values.

You might want to check out this post:

http://www.bennadel.com/blog/1820-Maintaining-ColdFusion-Query-Data-Type-Integrity-Throughout-The-Serialization-Life-Cycle.htm

It specifically talks about maintaining proper query cell values when deserializing from XML (WDDX).

Reply to this Comment

Am using coldfusion 5 and the first piece of code did not throw error - "Variable OBJFOO is undefined.".
What am looking for is a solution to memory leak issue and so am wanting that all the variables should be killed at the end of the code. Any suggestions ?

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.