| Contents > Developing ColdFusion MX Applications > Building and Using ColdFusion Components > Building ColdFusion components > Defining component methods 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:
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.
Note: For the following procedure to work, you must have the example applications installed with ColdFusion MX. For more information, see CFML Reference.
<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>
In the example, the cfargument attributes specify the following:
name attributes define the parameter namestype attribute for the lastName argument specifies that the parameter must be a text string. The type attribute for the cost argument specifies that the parameter must be a numeric value. These attributes validate the data before it is submitted to the database.required attributes indicate that the parameters are required or an exception will be thrown. The Arguments variable scope provides access to the parameter values.
|
|
||
| Contents > Developing ColdFusion MX Applications > Building and Using ColdFusion Components > Building ColdFusion components > Defining component methods Defining and using method parameters |
|
|
ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting
Version 6.1
Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.