
61
DEVELOPING
COLDFUSION 9 APPLICATIONS
The CFML Programming Language
Las
t
upda
te
d 8/5/2010
The Java
setVariable
Response interface method and C++
CCFX::SetVariable
method return data to the
Variables scope of the calling page. Therefore, they are equivalent to setting a Caller scope variable in a custom
ColdFusion tag.
Using scopes as structures
ColdFusion makes all named scopes available as structures. You cannot access the function-local scope for user-
defined functions (UDFs) that you define using CFScript as a structure.
You can reference the variables in named scopes as elements of a structure. To do so, specify the scope name as the
structure name and the variable name as the key. For example, if you have a MyVar variable in the Request scope, you
can reference it in either of the following ways:
Request.MyVar
Request["MyVar"]
Similarly, you can use CFML structure functions to manipulate the contents of the scope. For more information on
using structures, see “
Using Arrays and Structures
” on page 82.
Important:
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, place those variables in a structure in the Session scope, and then clear that structure. For example, place all
your application variables in Session.MyVars and then call
StructClear(Session.MyVars)
to clear the variables.
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 the following:
•
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.
•
You can use a
cfinput
tag with a
hidden
attribute to tell ColdFusion to display a helpful message to any user who
does not enter data in a required field. For more information on this technique, see “
Requiring users to enter values
in form fields
” on page 709.
Testing for a variable’s existence
Before relying on a variable’s existence in an application page, you can test to see if it exists by using the
IsDefined
function. To check whether a specific entry exists in an array, use the
ArrayIsDefined
function. To check whether a
specific key exists in a structure, use the StructKeyExists function.
For example, if you submit a form with an unsettled check box, the action page does not get a variable for the check
box. The following example from a form action page makes sure the Contractor check box Form variable exists before
using it:
<cfif IsDefined("Form.Contractor")>
<cfoutput>Contractor: #Form.Contractor#</cfoutput>
</cfif>
You must always enclose the argument passed to the
IsDefined
function in quotation marks. For more information
on the
IsDefined
function, see the
CFML Reference
.