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:
However, its value is "[undefined struct element]" and any attempt to reference it directly will throw the error:
Element BAR is undefined in OBJFOO.
I was trying to figure out how to do this, and this cleared everything up! Thank you!
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!
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#)>
You can always use structDelete() to clear variables as well (which might be a little more self-documenting).
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:
It specifically talks about maintaining proper query cell values when deserializing from XML (WDDX).
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 ?