0
getting started with cfc's - how do i pass a variable in?
Participant
,
/t5/coldfusion-discussions/getting-started-with-cfc-s-how-do-i-pass-a-variable-in/td-p/909556
Apr 19, 2008
Apr 19, 2008
Copy link to clipboard
Copied
hi - i'm trying to write my first cfc. i want it to get some
data from the database but cant seem to get it to accept a variable
that exists in the page i'm calling the cfc from (although oddly it
seems to be able to make use of form variables that exists in that
page - i can give more info on that weirdness if you want it)
i have this: (code attached)
but get this error:
"Element LOCATIONID is undefined in FORCFC."
what's that all about - i just set it on the line before i invoked the cfc - didn't i?
thanks in advance
i have this: (code attached)
but get this error:
"Element LOCATIONID is undefined in FORCFC."
what's that all about - i just set it on the line before i invoked the cfc - didn't i?
thanks in advance
TOPICS
Getting started
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/getting-started-with-cfc-s-how-do-i-pass-a-variable-in/m-p/909557#M83611
Apr 19, 2008
Apr 19, 2008
Copy link to clipboard
Copied
In your function you need cfargument tags.
Passing your argument to a function can be done a variety of ways. My preference is argumentcollection = somestructure.
Passing your argument to a function can be done a variety of ways. My preference is argumentcollection = somestructure.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Nick Way
AUTHOR
Participant
,
/t5/coldfusion-discussions/getting-started-with-cfc-s-how-do-i-pass-a-variable-in/m-p/909558#M83612
Apr 19, 2008
Apr 19, 2008
Copy link to clipboard
Copied
thanks Dan, that's what i thought - but when i change the
funcion to this
<cfcomponent displayname="coordinates cfc" hint="This is the CFC to get the x and y coordinates and locationname of a location from the locationid pass this as forcfc.locationid">
<!--- this function gets the x and y coordinates of a location from the locationid --->
<cffunction name="get_location_details" hint="gets the x and y coordinates and locationname of a location from the locationid pass this as forcfc.locationid" returntype="query">
<cfargument name="forcfc.locationid" />
<cfquery datasource="arj" name="rs_location" >
select locationname, xcoord, ycoord
from tbl_locations
where locationkey = #forcfc.locationid#
</cfquery>
<cfreturn rs_location>
</cffunction>
</cfcomponent>
(by adding this line - <cfargument name="forcfc.locationid" /> )
i get this error:
Invalid argument name.
The name forcfc.locationid used for an argument has illegal characters in it.
so does that mean i can't scope my variables when i use cfc's - that sounds a bit odd.
would passing the vars as a structure as you suggest get around this problem somehow?
forgive me but could you elaborate on how to do that (i dont know to make a scructure) and ive studies the cf docs on this but that doesnt seem to help either
at this rate i think i'll give cfc's a miss and just make an include that does what i want - but that sounds counter intuative as everyone says how important it is to use cfc's
thanks for anything else you can give me
cheers
Nick
<cfcomponent displayname="coordinates cfc" hint="This is the CFC to get the x and y coordinates and locationname of a location from the locationid pass this as forcfc.locationid">
<!--- this function gets the x and y coordinates of a location from the locationid --->
<cffunction name="get_location_details" hint="gets the x and y coordinates and locationname of a location from the locationid pass this as forcfc.locationid" returntype="query">
<cfargument name="forcfc.locationid" />
<cfquery datasource="arj" name="rs_location" >
select locationname, xcoord, ycoord
from tbl_locations
where locationkey = #forcfc.locationid#
</cfquery>
<cfreturn rs_location>
</cffunction>
</cfcomponent>
(by adding this line - <cfargument name="forcfc.locationid" /> )
i get this error:
Invalid argument name.
The name forcfc.locationid used for an argument has illegal characters in it.
so does that mean i can't scope my variables when i use cfc's - that sounds a bit odd.
would passing the vars as a structure as you suggest get around this problem somehow?
forgive me but could you elaborate on how to do that (i dont know to make a scructure) and ive studies the cf docs on this but that doesnt seem to help either
at this rate i think i'll give cfc's a miss and just make an include that does what i want - but that sounds counter intuative as everyone says how important it is to use cfc's
thanks for anything else you can give me
cheers
Nick
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/getting-started-with-cfc-s-how-do-i-pass-a-variable-in/m-p/909559#M83613
Apr 19, 2008
Apr 19, 2008
Copy link to clipboard
Copied
Give your arguments simple names. No periods.
CFCs are designed to be available to more than one calling template. Therefore they are islands of code unto themselves. They have nothing to do with calling templates other than receiveing arguments and returning something.
CFCs are designed to be available to more than one calling template. Therefore they are islands of code unto themselves. They have nothing to do with calling templates other than receiveing arguments and returning something.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Nick Way
AUTHOR
Participant
,
/t5/coldfusion-discussions/getting-started-with-cfc-s-how-do-i-pass-a-variable-in/m-p/909562#M83616
Apr 19, 2008
Apr 19, 2008
Copy link to clipboard
Copied
quote:
Originally posted by: Dan Bracuk
Give your arguments simple names. No periods.
so taking that forward then it means i cant scope my variables (that i want to be available to cfc's) - that's a bit of an odd design feature of cfc's then isn't it - i thought scoping variables was important?
i have to say that i will stick to doing what i do at the moment, which is (whenever i have a piece of code i use several times throughout my app) i stick it in a separate cfm doc (with nothing else in the template except that piece of code) and then just 'invoke' that code using <cfinclude...
what do you say to that?
v interested to hear your views - as i said everyone tells me cfc's are v important but a) their a bit weird to get my head around (compared to my cfinclude model above) b) now it seems i have to stop scoping my variables in order to use them (which surely is a step backwards isn't it)
thanks very much in advance - i hope someone can tell my cfc's are still the way to go 🙂
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Contributor
,
LATEST
/t5/coldfusion-discussions/getting-started-with-cfc-s-how-do-i-pass-a-variable-in/m-p/909565#M83619
Apr 23, 2008
Apr 23, 2008
Copy link to clipboard
Copied
After you check out the LiveDocs links that Adam posted,
check out my site. I've got a series on OOP with CF with code
examples. It starts here:
Intro to Object.cfc
Part 3, Injecting Data into Objects should cover your specific question.
HTH,
Adrian
Intro to Object.cfc
Part 3, Injecting Data into Objects should cover your specific question.
HTH,
Adrian
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Participant
,
/t5/coldfusion-discussions/getting-started-with-cfc-s-how-do-i-pass-a-variable-in/m-p/909560#M83614
Apr 19, 2008
Apr 19, 2008
Copy link to clipboard
Copied
Can you also use cfparam as well in cfcs instead of arguments
Dan?
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/getting-started-with-cfc-s-how-do-i-pass-a-variable-in/m-p/909561#M83615
Apr 19, 2008
Apr 19, 2008
Copy link to clipboard
Copied
quote:
Originally posted by: Hydrowizard
Can you also use cfparam as well in cfcs instead of arguments Dan?
To do what?
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/getting-started-with-cfc-s-how-do-i-pass-a-variable-in/m-p/909563#M83617
Apr 19, 2008
Apr 19, 2008
Copy link to clipboard
Copied
Inside your cfc, you can use the arguments scope, the this
scope, the property scope, or the variables scope.
cfc's are not the only way to reuse code. You can also write custom tags, or included files. Also since some cfc's are nothing more than a bunch of udfs, you can put these udfs into an included file.
cfc's are not the only way to reuse code. You can also write custom tags, or included files. Also since some cfc's are nothing more than a bunch of udfs, you can put these udfs into an included file.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/getting-started-with-cfc-s-how-do-i-pass-a-variable-in/m-p/909564#M83618
Apr 19, 2008
Apr 19, 2008
Copy link to clipboard
Copied
I recommend you start by reading the docs:
http://livedocs.adobe.com/coldfusion/8/buildingComponents_01.html
(read it THOROUGHLY).
Then read the related docs on all the various tags thoroughly too:
<cfcomponent>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_c_10.html
<cffunction>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_f_21.html#2852457
<cfargument>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_a-b_6.html#2855190
<cfreturn>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_r-s_09.html#2498057
<cfinvoke>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_i_10.html#4001127
<cfinvokeargument>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_i_11.html#3014048
<cfobject>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_m-o_08.html#2820239
createObect()
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_in-k_17.html#5183878
The info in the docs is pretty complete, and all you should need to get
your brain around it.
--
Adam
http://livedocs.adobe.com/coldfusion/8/buildingComponents_01.html
(read it THOROUGHLY).
Then read the related docs on all the various tags thoroughly too:
<cfcomponent>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_c_10.html
<cffunction>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_f_21.html#2852457
<cfargument>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_a-b_6.html#2855190
<cfreturn>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_r-s_09.html#2498057
<cfinvoke>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_i_10.html#4001127
<cfinvokeargument>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_i_11.html#3014048
<cfobject>
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_m-o_08.html#2820239
createObect()
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_in-k_17.html#5183878
The info in the docs is pretty complete, and all you should need to get
your brain around it.
--
Adam
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

