Adobe COLDFUSION 9 Manual page 79

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
The CFML Programming Language
This does not mean that you must always avoid dynamic evaluation. However, given the substantial performance costs
of dynamic evaluation, first ensure that one of the following techniques cannot serve your purpose:
• An array (using index variables)
• Associative array references containing expressions to access structure elements
• Dynamically generated variable names
Dynamic variable naming without dynamic evaluation
While ColdFusion does not always allow you to construct a variable name inline from variable pieces, but it does allow
the most common uses.
Using number signs to construct a variable name in assignments
You can combine text and variable names to construct a variable name on the left side of a
example, the following code sets the value of the variable Product12 to the string "Widget":
<cfset ProdNo = 12>
<cfset "Product#ProdNo#" = "Widget">
To construct a variable name this way, all the text on the left side of the equal sign must be in quotation marks.
This usage is less efficient than using arrays. The following example has the same purpose as the previous one, but
requires less processing:
<cfset MyArray=ArrayNew(1)>
<cfset prodNo = 12>
<cfset myArray[prodNo] = "Widget">
Dynamic variable limitation
When you use a dynamic variable name in quotation marks on the left side of an assignment, the name must be either
a simple variable name or a complex name that uses object.property notation (such as MyStruct.#KeyName#). You
cannot use an array as part of a dynamic variable name. For example, the following code generates an error:
<cfset MyArray=ArrayNew(1)>
<cfset productClassNo = 1>
<cfset productItemNo = 9>
<cfset "myArray[#productClassNo##productItemNo#]" = "Widget">
However, you can construct an array index value dynamically from variables without using quotation marks on the left
side of an assignment. For example, the preceding sample code works if you replace the final line with the following line:
<cfset myArray[#productClassNo# & #productItemNo#] = "Widget">
Dynamically constructing structure references
The ability to use associative array notation to reference structures provides a way for you to use variables to
dynamically create structure references. (For a description of associative array notation, see
page 93.) Associative array structure notation allows you to use a ColdFusion expression inside the index brackets. For
example, if you have a productName structure with keys of the form product_1, product_2 and so on, you can use the
following code to display the value of productName.product_3:
<cfset prodNo = 3>
<cfoutput>
Product_3 Name: #ProductName["product_" & prodNo]#
<cfoutput>
For an example of using this format to manage a shopping cart, see
"Example: a dynamic shopping
Last updated 8/5/2010
74
assignment. For
cfset
"Structure
notation" on
cart" on page 79.

Advertisement

Table of Contents
loading

Table of Contents