
238
Chapter 11: Building and Using ColdFusion Components
Defining and using method parameters
To define the component method parameter, use the
cfargument
tag in the
cffunction
tag
body. To define multiple parameters, use multiple
cfargument
tags. You can create CFC methods
that do not use
cfargument
tags, for example, if you use positional parameters in your methods.
However, most CFC methods use the
cfargument
tag. The tag names a parameter and lets you
specify the following:
•
Whether the parameter is required
•
The type of data that is required
•
A default argument value
•
Display name and hint metadata for CFC introspection
For detailed reference information on the
cfargument
tag, see
CFML Reference
.
To access the parameter values in the component method definition, use structure- or array-like
notation with the Arguments scope. The following example refers to the lastName argument as
Arguments.lastName; it could also refer to it as Arguments[1]. In addition, you can access
arguments directly using pound signs, such as #lastName#; however, it is better programming
practice to identify the scope. Also, you can use Array- or structure-like notation, which lets you
loop over multiple parameters.
For more information on the Arguments scope, see
“The Arguments scope” on page 243
.
Note:
For the following procedure to work, you must have the example applications installed with
ColdFusion MX. For more information, see
CFML Reference
.
To define parameters in the component method definition:
•
Create a new component with the following contents, and save it as corpQuery.cfc in a
directory under your web root directory.
<cfcomponent>
<cffunction name="getEmp">
<cfargument name="lastName" type="string" required="true"
hint="Employee last name">
<cfquery name="empQuery" datasource="ExampleApps" dbtype="ODBC">
SELECT LASTNAME, FIRSTNAME, EMAIL
FROM
tblEmployees
WHERE LASTNAME LIKE '#Arguments.lastName#'
</cfquery>
<cfoutput>Results filtered by #Arguments.lastName#:</cfoutput><br>
<cfdump var=#empQuery#>
</cffunction>
<cffunction name="getCat" hint="Get items below specified cost">
<cfargument name="cost" type="numeric" required="true">
<cfquery name="catQuery" datasource="ExampleApps" dbtype="ODBC">
SELECT ItemName, ItemDescription, ItemCost
FROM
tblItems
WHERE ItemCost <= #Arguments.cost#
</cfquery>
<cfoutput>Results filtered by #Arguments.cost#:</cfoutput><br>
<cfdump var=#catQuery#>
</cffunction>
</cfcomponent>
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: ......