78
Chapter 3: Using ColdFusion Variables
Caution:
Do not call
StructClear
(Session)
to clear session variables. This deletes the
SessionID
,
CFID
, and
CFtoken
built-in variables, effectively ending the session. If you want to use
StructClear
to
delete your application variables, put those variables in a structure in the Session scope, then clear
that structure. For example, put all your application variables in Session.MyVars and then call
StructClear(Session.MyVars)
to clear the variables.
Using the This scope outside CFCs (advanced topic)
The This scope is specifically designed for use with ColdFusion Components (CFCs). For
information on CFCs and the This scope, see
Chapter 11, “Building and Using ColdFusion
Components,” on page 219
. However, you can also use the This scope without having a CFC if
you do the following:
•
Create a UDF using the
cffunction
tag.
•
Assign the UDF to a containing object such as a structure or persistent scope.
In this case, the This scope inside the function is a reference to the containing object, and you
can therefore access the same variables.
•
Specify This as the prefix, inside the function.
•
Specify the containing object name as the prefix, outside the function.
The following code shows how this works:
<cffunction name="TestFunction" >
<cfparam name="This.foo" default="Original This.foo">
dumping This inside the function:<br>
<cfdump var="#This#">
</cffunction>
First, just call the function<br>
<cfset TestFunction()>
Right now, the This variable is NOT a scope reference, just a structure.
<br><br>
Now put function in a structure and dump the structure<br>
Note that there is no foo variable in the structure.
<cfset newStruct.TestFunction=TestFunction>
<cfdump var="#newStruct#">
<br>
Now change newStruct.foo and call the function <br>
<cfset newStruct.foo="New This.foo">
<cfset newStruct.TestFunction()>
Note that now we've changed the foo variable from outside the function,<br>
And the function itself is part of the This scope!
Ensuring variable existence
ColdFusion generates an error if you try to use a variable value that does not exist. Therefore,
before you use any variable whose value is assigned dynamically, you must ensure that a variable
value exists. For example, if your application has a form, it must use some combination of
requiring users to submit data in fields, providing default values for fields, and checking for the
existence of field variable values before they are used.
There are several ways to ensure that a variable exists before you use it, including:
•
You can use the
IsDefined
function to test for the variable’s existence.
•
You can use the
cfparam
tag to test for a variable and set it to a default value if it does not exist.
Summary of Contents for ColdFusion MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......