Destroying ColdFusion Variables Using JavaCast()
Posted July 31, 2006 at 3:22 PM
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:
Launch code in new window » Download code as text file »
- <!--- 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:
Launch code in new window » Download code as text file »
- <!--- 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:
| | | | ||
| | | |||
| | | |
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.
Download Code Snippet ZIP File
Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Newer Post
Client Flattery Is Good For The Soul
Older Post
Undefined Values In Manually-Built Query Are NULL
Reader Comments
I was trying to figure out how to do this, and this cleared everything up! Thank you!




