Destroying ColdFusion Variables Using JavaCast()

Posted July 31, 2006 at 3:22 PM by Ben Nadel

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

Aug 25, 2006 at 11:15 AM // reply »
1 Comments

I was trying to figure out how to do this, and this cleared everything up! Thank you!


Sep 10, 2010 at 2:23 AM // reply »
3 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!


Sep 23, 2010 at 3:06 AM // reply »
10 Comments

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!


Sep 23, 2010 at 2:22 PM // reply »
11,314 Comments

@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).


Post A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 20, 2013 at 3:15 AM
A Billion Wicked Thoughts By Ogi Ogas And Sai Gaddam
nice post i love it thanks 4 u :) ... read »
seb
Jun 20, 2013 at 2:32 AM
Working With Inherited Collections In AngularJS
@mike, @ben, The best article about scope and prototypal prototypical inheritance in angularjs is http://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical- ... read »
Jun 20, 2013 at 2:17 AM
ColdFusion NumberFormat() Exploration
Nice read thanks Ben, Is there a way to mask a negative number? Long story short in the finance sector when you go 'short' on a stock you want the price to fall this is a good thing because you are ... read »
Jun 20, 2013 at 1:09 AM
The Beauty Of The jQuery Each() Method
my html code : <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="nss.js"> ... read »
Jun 19, 2013 at 11:31 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Ben, bunch to learn indeed, but thats fun part : ) ... read »
Jun 19, 2013 at 10:41 PM
Referencing ColdFusion Query Columns In A Loop Using Both Array And Dot Notation
Burdock-roots Are you going fat day by day? You need to be good for your family and make some money too. So we bring for you a best product that helps you to be more energetic every day. You will b ... read »
Jun 19, 2013 at 9:52 PM
Working With Inherited Collections In AngularJS
I recognize the applicability of your solution, and how easy it makes to share data across multiple views or even "submodules" of rather simple application. But it seems to me that it creat ... read »
Jun 19, 2013 at 9:38 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Alesei, Glad you like it. Even after working with AngularJS for months, I still get a bunch of unexpected, "$digest is already in progress". So hard to debug sometimes! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools