[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

How do I undefine a variable in CF 5.0?

How can I completely remove/annihilate/undefine a defined variable that would cause it to then fail an 'IsDefined()' test?

Example:

<cfset Application.TempVar = "test">

<!--- do what? to cause this if test to fail? --->

<cfif IsDefined( "Application.TempVar" )>
   <cfoutput>OK</cfoutput>
<cfelse>
   <!--- want to wind up HERE --->
   <cfoutput>but I thought it existed!</cfoutput>
</cfif>

Remember, CF5! Thanks!
0
tbaum
Asked:
tbaum
  • 2
1 Solution
 
mosphatCommented:
I can't think of a reason to use isDefined(). There's an alternative: structKeyExists(). And since all variables are scoped in ColdFusion (even if you don't define a scope, it ends up in the variables scope) and structKeyExists() is a tiny bit faster than isDefined(), why not use it all the time?

So, instead of IsDefined("Application.TempVar"), use structKeyExists(application, "tempVar").
And to answer your question: structDelete(application, "tempVar") will remove, annihilate, undefine and even delete the key from the application struct.

HTH,
Ruud
0
 
kkhippleCommented:
my little workaround to that problem is to check if the variable exists and to check it a value for it exists


<CFIF IsDefined( "Application.TempVar" )> AND Application.TempVar NEQ "">



or something to that affect
0
 
tbaumAuthor Commented:
mosphat, GOOD ANSWER, now do me one better...

You will get the points, now for the bonus round...

That will work find for Application and Session vars, but what about just plan-old local vars?  How would you go about destroying them?

Thanks.
0
 
mosphatCommented:
Like I said: even if you don't define a scope, it ends up in the variables scope.

This variable is actually set in the variables scope:
<cfset plain_old_local_var = "test">

So you can delete it like this:
<cfset structDelete(variables, "plain_old_local_var")>
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now