Listing 1
<!---
The UDF is defined in this block of CFSCRIPT.
Two arguments are passed to the function, a temperature and
the scale that it is in. The temperature is checked to
make sure it's a number (the value "NAN" is returned and
processing ends if it is not). Then the calculation is
processed based on the scale being "F" or "C." If the
scale given is neither "F" nor "C," the message "Not a
valid scale" is returned.
--->
<cfscript>
function TempConvert(ATemp, ItsScale){
if (not IsNumeric(ATemp)) return "NAN";
if (UCase(ItsScale) eq "F")
// temp given in Fahrenheit. Convert to Celsius
degs = (ATemp - 32.0) * (5.0/9.0);
else if (UCase(ItsScale) eq "C")
// temp given in Celsius. Convert to Fahrenheit
degs = (ATemp * 9.0/5.0) + 32;
else
degs = "Not a valid Scale";
;
return degs;
}
</cfscript>
Listing 2
<cfcomponent>
<cffunction name="doTempConvert" returntype="string"
access="remote" output="false">
<!--- // two required arguments --->
<cfargument name="ATemp" required="true" type="string">
<cfargument name="ItsScale" required="true" type="string" >
<cfif not isNumeric(ATemp)>
<cfset degs = "NAN">
<cfreturn degs>
</cfif>
<cfscript>
if (UCase(ItsScale) eq "F")
// temp given in Fahrenheit. Convert to Celsius
degs = (ATemp - 32.0) * (5.0/9.0);
else if (UCase(ItsScale) eq "C")
// temp given in Celsius. Convert to Fahrenheit
degs = (ATemp * 9.0/5.0) + 32;
else
degs = "Not a valid Scale";
</cfscript>
<cfreturn degs>
</cffunction>
</cfcomponent>
Listing 3
<!--- // set some local variables with values for conversion --->
<cfset temp = 24>
<cfset scale = "C">
<!--- // invoke the web service for temperature conversion --->
<cfinvoke method="doTempConvert"
returnvariable="degs"
webservice="http://localhost:8500/cfdj/com/tempconvert.cfc?wsdl">
<cfinvokeargument name="ATemp" value="#temp#">
<cfinvokeargument name="ItsScale" value="#scale#">
</cfinvoke>
<!--- // output reponse from Web service --->
<cfoutput>
Original temp: #temp# #scale# <br>
Converted to: #degs# F <br>
</cfoutput>
Listing 4
<cfscript>
// set some local variables with values for conversion
temp = 24;
scale = "C";
// invoke the web service for temperature conversion
ws = createObject("webservice",
"http://localhost:8500/cfdj/com/tempconvert.cfc?wsdl");
degs = ws.doTempConvert(temp, scale);
writeOutput("Original Temp:" & temp & " " & scale & "<br>");
writeOutput("Converted to: " & degs & " F");
</cfscript>