Destroying ColdFusion Variables Using JavaCast()

Posted July 31, 2006 at 3:22 PM

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:

 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:


 
 
 

 
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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

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!


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »