Skip to main content
Known Participant
June 6, 2011
Answered

Using CFC's

  • June 6, 2011
  • 2 replies
  • 2691 views

Hello,

I have a simple CFC defined as such. And named ct.cfc

<cfcomponent>
    <!--- Celsius to Fahrenheit conversion method. --->
    <cffunction name="ctof" output="false">
        <cfargument name="temp" required="yes" type="numeric">
        <cfreturn ((temp*9)/5)+32>
    </cffunction>
</cfcomponent>

Then I have a .cfm file named test.cfm and both the cfc file and the cfm file are in the same directory. The test.cfm file is below:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<cfscript>
  obj1 = createObject("component","ct");
  obj1.ctof(temp=100);
</cfscript>

</body>
</html>

I can't seem to figure out how to print the output of obj1 to the screen. I have tried obj1.temp and that does not work. Also all of the code above does not throw an error. It is only when I try to cfoutput the value I run into problems. So how do you print the value? It should be 212.

Thanks,

Jim

This topic has been closed for replies.
Correct answer Owainnorth
Use this as an example.

<cfset x=5>

then 200 lines later I say:

<cfoutput>#x#</cfoutput>

Right, so how about:

<cfset x = obj1.ctof(temp=100) >

then 200 lines later I say:

<cfoutput>#x#</cfoutput>


Is that what you're trying to do? That'll set the variable "x" to be a copy of the value returned from your CFC's method. You can then repeatedly use said variable without actually running the method again.

2 replies

Known Participant
June 6, 2011

you may want to review this https://github.com/mhenke/CFML-in-100-minutes

jimfid45Author
Known Participant
June 6, 2011

Mike thanks for the link looking now

Owainnorth
Inspiring
June 6, 2011

You're within a CFSCRIPT block, so you cannot use CFOUTPUT; you need the script version - writeOutput() - instead.

writeOutput(obj1.ctof(temp=100)) ;

Will put the return variable out to the screen.

jimfid45Author
Known Participant
June 6, 2011

Owain thanks for the answer. I should have been more specific. What I want to do is say output the variable later I know obj1 in some way has the value 212.

So what I want to do is get 212 to the screen. I do not want to output the variable as I define it.  Use this as an example.

<cfset x=5>

then 200 lines later I say:

<cfoutput>#x#</cfoutput>

Thanks,

Jim

Owainnorth
OwainnorthCorrect answer
Inspiring
June 6, 2011
Use this as an example.

<cfset x=5>

then 200 lines later I say:

<cfoutput>#x#</cfoutput>

Right, so how about:

<cfset x = obj1.ctof(temp=100) >

then 200 lines later I say:

<cfoutput>#x#</cfoutput>


Is that what you're trying to do? That'll set the variable "x" to be a copy of the value returned from your CFC's method. You can then repeatedly use said variable without actually running the method again.