Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cfcs and udfs

Status
Not open for further replies.

forumposters

Programmer
Aug 31, 2006
61
US
As a new coldfusion developer, I'm not sure when to make a function a udf or a cfc. Also, I'm not sure how to convert one to the other. Can anyone describe when to make it a udf or a cfc and how to convert one to the other?
 
What are you trying to accomplish? A UDF is a function that you create - the are usually used to modify display output in some way. A CFC is a block of code that you can refer to and use repeatedly without having to rewrite it.

Cheers,

Bluetone
 
in my eyes, a udf is old school....

you can create a udf.cfc page with all your functions in it...
instanciate it
Code:
<script>
objUDFs = createObject('component','cfcs.udfs');
</script>

then you can call your function like you would with a udf...

Code:
objUDFs.functionName(argument1,argument2,etc....);

they are almost the same except you can use tags & script in a CFS vs. only script in a UDF...


----------------------------------
htower
if( !succeed ) try( );
 
& script in a CFS vs. only script in a UDF..."

You can build a UDF with either....

udfs are still very useful a function is not a method! You use ColdFusion build in functions all the time, creating UDFs still has a place.

Kevin

Phase 1: Read the CFML Reference
Phase 2: ???
Phase 3: Profit!
 
im not saying a udf doesnt have it's place anymore... im just saying it is cleaner to build them like a componet..

Code:
<cffunction name="CapFirst" returntype="string" output="false">
	<cfargument name="str" type="string" required="true" />
	
	<cfset var newstr = "" />
	<cfset var word = "" />
	<cfset var separator = "" />
	
	<cfloop index="word" list="#arguments.str#" delimiters=" ">
		<cfset newstr = newstr & separator & UCase(left(word,1)) />
		<cfif len(word) gt 1>
			<cfset newstr = newstr & right(word,len(word)-1) />
		</cfif>
		<cfset separator = " " />
	</cfloop>

	<cfreturn newstr />
</cffunction>
or
Code:
<cffunction name="capFirst"  returntype="string" access="public" output="No">
		<cfargument name="capTheWord" required="yes" type="string">
		<cfscript>
			var str = lcase(arguments.capTheWord);
			var newstr = '';
			var word = '';
			var i = 1;
			var strlen = listlen(str,' ');
			for(i=1;i lte strlen;i=i+1){
				word = ListGetAt(str,i,' ');
				newstr = newstr & UCase(Left(word,1));
				if(len(word) gt 1) newstr = newstr & Right(word,Len(word)-1);
				if(i lt strlen) newstr = newstr & ' ';
			}
		</cfscript>
		<cfreturn newstr />
	</cffunction>
vs.

Code:
<CFSCRIPT>
function CapFirst(str) {
	var newstr = "";
	var word = "";
	var i = 1;
	var strlen = listlen(str," ");
	for(i=1;i lte ListLen(str," ");i=i+1) {
		word = ListGetAt(str,i," ");
		newstr = newstr & UCase(Left(word,1));
		if(len(word) gt 1) newstr = newstr & Right(word,Len(word)-1);
		if(i lt strlen) newstr = newstr & " ";
	}
	return newstr;
}
</CFSCRIPT>


----------------------------------
htower
if( !succeed ) try( );
 
I think i see what you mean.

That's where I said you can build udfs with script of tags.

Either way, it's still a udf - it's never a method of a component until wrapped in the cfcomponent tags.

the use of cfscript vs tags doesn't change anything here as far as udf's go.

carry on.

Kevin

Phase 1: Read the CFML Reference
Phase 2: ???
Phase 3: Profit!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top