> Can someone please explian when it would be more advantageous to use
> setVariables instead of cfset?
The SetVariable function allows you to create variable names at runtime,
which can be very valuable. For example, if you write a custom tag, you
might want it to return a value to a variable specified by the user. This is
typically how user-defined functions work in other languages; CF's closest
analog to a user-defined function is a custom tag. In JavaScript, I can do
this:
function dosomething() {
return somevalue;
}
and a caller of the function can receive the return value in the variable of
their choice:
var x = dosomething();
Using CFML custom tags, there is no direct analog to "return", but your
custom tag can write directly to the Caller scope (which corresponds to the
local scope of the calling page):
<cfset caller.val = somevalue>
The problem here is that, by doing this, the custom tag is deciding what
variable in the calling page will hold the returned value, instead of the
user of the custom tag. So, if the caller of the custom tag is already using
the variable "val", it will be overwritten by the variable from the custom
tag. A better approach would be to allow the user of the custom tag to name
the variable that will be written to:
<cf_dosomething return="somevar">
This requires that the custom tag write to the variable, but the custom tag
won't know until runtime what that variable should be named. That's where
you'd use SetVariable:
<cfset rs = SetVariable("Caller." & Attributes.Return, somevalue)>
You could actually use CFSET to get the same result, using a trick of the
language:
<cfset "Caller.#Attributes.Return#" = somevalue>
I'd recommend against this, though, as I'd recommend against any
non-standard use of the language; it works now, but there's no guarantee
that it'll work in future versions.
Dave Watts, CTO, Fig Leaf Software
http://www.figleaf.com/
voice: (202) 797-5496
fax: (202) 797-5444
------------------------------------------------------------------------------
Archives: http://www.mail-archive.com/[email protected]/
To Unsubscribe visit
http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk or send a
message to [EMAIL PROTECTED] with 'unsubscribe' in the body.