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 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
Jun 18, 2013 at 3:39 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
Hi Ben, THANKS! While not bleeding edge, it is new to me & I like learning new things every day! ... read »
Jun 18, 2013 at 12:30 PM
Disabling Auto-Correct And Auto-Capitalize Features On iPhone Inputs
Also spellcheck="false" should be mentioned as part of html5 specs ... read »
Jun 18, 2013 at 8:40 AM
Using Named Functions Within Self-Executing Function Blocks In Javascript
Hi Ben, you forgot to mention the most important thing for named self-executing functions - they can be referenced by name ONLY inside their execution context (which is parens in this case), it mean ... read »
dee
Jun 18, 2013 at 7:01 AM
My Safari Browser SQLite Database Hello World Example
hai ben, this program is really good i could understand the concept but i dint know how to save it and how to open it as you have done in the video can u give that details pls ... read »
Jun 18, 2013 at 6:04 AM
Clearing Inline CSS Properties With jQuery
Thanks a lot for for post! It helped me a lot... after being stuck since 24 hrs.. found solution from your post. Thanks again! ... read »
Jun 18, 2013 at 2:31 AM
SOTR 2013 - The Best Conference I Never Went To
I keep watching it, should keep me happily distracted until SotR14 ;) ... read »
Jun 17, 2013 at 9:45 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, As I was reading what you wrote, it occurred to me that maybe I do something similar to that in some of my client-side code. In an application I'm working on, there are a bunch of unrelated ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools