Setting Values

Team-Fly    

ColdFusion® MX: From Static to Dynamic in 10 Steps
By Barry Moore
Table of Contents
Step 2.  Using Variables


In ColdFusion, you create variables by giving them a name and assigning them a value. In this step, we will concentrate on the most commonly used ways to assign a value to a variable.

Using <CFSET>

As we have already seen, the <CFSET> tag can be used to assign a value to a variable. The <CFSET> tag is quick and easy to use and has no attributes other than the variable name and its assigned value. For example, the line of code

 <CFSET Qty=2>

would create a variable named Qty and would set the value of that variable to the numerical value of 2. If the variable already exists, you can use <CFSET> to reassign the variable a different value. For example, the code

 <CFSET Qty=2>  <CFSET Qty=3>  <CFOUTPUT>You have ordered #Qty# jars.</CFOUTPUT>

would first set the value of the Qty variable to 2. Because the Qty variable already exists, the second line of code would then reassign the variable's value to 3. The third line of code would then output You have ordered 3 jars.

You can also perform calculations inside the <CFSET> tag when assigning a value. For example, the code

 <CFSET Qty=2>  <CFSET Qty=Qty + 1>  <CFOUTPUT>You have ordered #Qty# jars.</CFOUTPUT>

would produce the same result as the previous code. The second line would take the current value of the Qty variable (in this case 2), add 1 to it, and assign that value (3) back to the Qty variable. This is a common way to increment the value of a variable.

Using <CFPARAM>

Trying to use or output a variable that has not yet been created will cause an error. Figure 2.1 shows you the error output that ColdFusion will return if you try to use the following line of code without first creating the Qty variable.

Figure 2.1. The error message that results from failing to create variable.

graphics/02fig01.gif

 <CFOUTPUT>You have ordered #Qty# jars.<CFOUTPUT>

Because we want to avoid errors, it is good practice to check whether a variable exists before you try to use it. Normally, the code would flow something like this.

  1. Check to see if a variable currently exists.

  2. If it does, use the variable's current value.

  3. If the variable does not exist, assign it some default value.

  4. Display or use the variable.

The <CFPARAM> tag encapsulates all this logic into one line of code. The <CFPARAM> tag takes three attributes, as outlined in Table 2.4.

Table 2.4. <CFPARAM> Tag Attributes

Attribute

Description

NAME (required)

The name of the variable to be checked, including scope prefix.

DEFAULT (optional)

The default value you would like to assign to the variable if it does not already exist.

TYPE (optional)

The type of data that the variable should contain. [*]

[*] (See the "Language Reference" section of www.LearnColdFusionMX.com for possible values.)

For example, in the code

 <CFPARAM NAME="Qty" DEFAULT=1>  <CFOUTPUT>Quantity ordered is #Qty#.</CFOUTPUT>

the first line would check for the existence of a variable named Qty. Because this variable has not yet been created, it will be created by the <CFPARAM> tag and will be assigned the default value of 1. The second line of code then outputs Quantity ordered is 1.

If a variable with the same name already exists, the <CFPARAM> tag is ignored. For example, in the code

 <CFSET Qty=2>  <CFPARAM NAME="Qty" DEFAULT=1>  <CFOUTPUT>Quantity ordered is #Qty#.</CFOUTPUT>

the first line of code sets the value of the Qty variable to the numeric value of 2. On the second line, the <CFPARAM> tag checks for the existence of a variable named Qty. Because this variable already exists, the <CFPARAM> tag is ignored, and the program flow continues on to the third line, which outputs Quantity ordered is 2.

The <CFPARAM> tag is a quick and simple way to check for the existence of variables before you attempt to use them. It will come in very handy when passing variables from one page to the next, such as when processing form data.


    Team-Fly    
    Top
     



    ColdFusion MX. From Static to Dynamic in 10 Steps
    ColdFusion MX: From Static to Dynamic in 10 Steps
    ISBN: 0735712964
    EAN: 2147483647
    Year: 2002
    Pages: 140
    Authors: Barry Moore

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net