views:

402

answers:

2

Is it possible to undefine a variable in ColdFusion?

For example, something like this:

<cfset myVar = "lsajflksd" />
<cfoutput>
  <p>myVar is Defined? #IsDefined("myVar")#</p> <!--- Prints YES --->
</cfoutput>
<cfset Undefine(myVar) /> <!--- Doesn't exist... --->
<cfoutput>
  <p>myVar is Defined? #IsDefined("myVar")#</p> <!--- I want it to print NO --->
</cfoutput>
+19  A: 
<cfset StructDelete(Variables, "myVar") />

Variables is the default scope for most variables in most contexts.

Justice
that works, thanks!
Kip
Note that this is a recent feature. Older versions of CF can't do this.
Al Everett
Sorry. All I know is Adobe CF 8.01.
Justice
Though it should work all the way back to MX6 at least.
Leigh
+4  A: 

FYI...

<cffunction name="voidFunc" returntype="void">
</cffunction>

<cfset myVar = voidFunc()>
<cfoutput>#IsDefined("myVar")#</cfoutput>    <!--- will show NO --->

I found out from this blog entry: cfinvoke destroys returnVariable for methods that return void

Henry
interesting....
Kip