ColdFusion 9.0 Resources |
Defining and using method parametersYou pass data to a method by using parameters. To define a component method parameter, use the cfargument tag in the cffunction tag body. To define multiple parameters, use multiple cfargument tags. The tag names a parameter and lets you specify the following:
Note: 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.
Example: convertTemp.cfcThe convertTemp.cfc file consists of the following: <cfcomponent> <!--- Celsius to Fahrenheit conversion method. ---> <cffunction name="ctof" output="false"> <cfargument name="temp" required="yes" type="numeric"> <cfreturn ((temp*9)/5)+32> </cffunction> <!--- Fahrenheit to Celsius conversion method. ---> <cffunction name="ftoc" output="false"> <cfargument name="temp" required="yes" type="numeric"> <cfreturn ((temp-32)*5/9)> </cffunction> </cfcomponent> Reviewing the codeThe convertTemp CFC contains two methods that convert temperature. The following table describes the code and its function:
Example: tempConversion.cfmThe ColdFusion page tempConversion.cfm is an HTML form in which the user enters the temperature to convert, and selects the type of conversion to perform. When the user clicks the Submit button, ColdFusion performs the actions on the processForm.cfm page. The file tempConversion.cfm, which is in the same directory as convertTemp.cfc, consists of the following: <cfform action="processForm.cfm" method="POST"> Enter the temperature: <input name="temperature" type="text"><br> <br> Select the type of conversion:<br> <select name="conversionType"> <option value="CtoF">Celsius to Farenheit</option> <option value="FtoC">Farenheit to Celsius</option> </select><br><br> <input name="submitform" type="submit" value="Submit"> </cfform> Example: processForm.cfmThe ColdFusion page processForm.cfm calls the appropriate component method, based on what the user entered in the form on the tempConversion.cfm page. Place it in the same directory as convertTemp.cfc. <cfif #form.conversionType# is "CtoF"> <cfinvoke component="convertTemp" method="ctof" returnvariable="newtemp" temp=#form.temperature#> <cfoutput>#form.temperature# degrees Celsius is #newtemp# degrees Farenheit.</cfoutput> <cfelseif #form.conversionType# is "FtoC"> <cfinvoke component="convertTemp" method="ftoc" returnvariable="newtemp" temp=#form.temperature#> <cfoutput>#form.temperature# degrees Fahrenheit is #newtemp# degrees Celsius.</cfoutput> </cfif> Reviewing the codeThe file processForm.cfm invokes the appropriate component method. The following table describes the code and its function:
To run the example, display the tempConversion.cfm page in your browser. When you enter a value in the text box of the form, the value is stored in the form.temperature variable. Processing is then performed on the processForm.cfm page, which refers to the value as form.temperature. When you invoke either method, the cfinvoke tag assigns the value form.temperature to temp; temp is the argument specified in the cfargument tag of the appropriate method. The appropriate method in the convertTemp component performs the necessary calculations and returns the new value as newtemp. For detailed reference information on the cfargument tag, see the 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 number (#) signs, such as #lastname#; however, it is better programming practice to identify the scope (for example, #Arguments.lastname#). 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. Define parameters in the component method definitionCreate a 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"> <cfset var empQuery=""> <cfquery name="empQuery" datasource="cfdocexamples"> SELECT LASTNAME, FIRSTNAME, EMAIL FROM tblEmployees WHERE LASTNAME LIKE '#Arguments.lastName#' </cfquery> <!--- Use cfdump for debugging purposes. ---> <cfdump var=#empQuery#> </cffunction> <cffunction name="getCat" hint="Get items below specified cost"> <cfargument name="cost" type="numeric" required="true"> <cfset var catQuery=""> <cfquery name="catQuery" datasource="cfdocexamples"> SELECT ItemName, ItemDescription, ItemCost FROM tblItems WHERE ItemCost <= #Arguments.cost# </cfquery> <!--- Use cfdump for debugging purposes. ---> <cfdump var=#catQuery#> </cffunction> </cfcomponent> In the example, the cfargument attributes specify the following:
|