[thelist] CF -- have a Q need a Tip to clean-up my webs.
Joshua Olson
joshua at alphashop.com
Fri Oct 12 19:54:55 CDT 2001
: I suspect I know... reference vs. value assignment in cfset.
:
: But, I need to _know_.
My experience has been that simple values (the only of which is String in
CF) are assigned by value. Thus,
<cfset a = "1">
<cfset b = a>
<cfset b = "2">
<cfoutput>#a#, #b#</cfoutput>
yields:
1,2
However, complex variables (Structs, arrays, and queries) are all passed by
reference. In SP 2 of 4.5.1 (or something like that) they added StructCopy
which does create of copy of the structure. I know of no such function for
Array and Queries.
Actually, I take that back, I see a function called Duplicate in the help.
It's new to me, but the help file says:
"Returns a clone, also known as a deep copy, of a variable. The copied
variable contains no reference to the original variable."
And goes on:
"This function is very useful in duplicating complex structures, including
nested structures and queries.
Note You cannot duplicate a COM, CORBA or JAVA object returned from the
CFOBJECT tag or the CreateObject function. If any element in an array or
field of a structure is a COM, CORBA, or JAVA object, you cannot duplicate
the array or structure. If you try to duplicate an object of this sort, an
exception is thrown. "
<!--- This example shows the use of Duplicate --->
<HTML>
<HEAD>
<TITLE>
Duplicate Example
</TITLE>
</HEAD>
<H3>Duplicate Example</H3>
<CFSET s1=StructNew()>
<CFSET s1.nested = StructNew()>
<CFSET s1.nested.item = "original">
<CFSET copy=StructCopy(s1)>
<CFSET clone=Duplicate(s1)>
<!--- modify the original --->
<CFSET s1.nested.item = "modified">
<cfoutput>
<P>The copy contains the modified value: #copy.nested.item#</P>
<P>The duplicate contains the original value: #clone.nested.item#</P>
</cfoutput>
</BODY>
</HTML>
HTH,
-joshua
More information about the thelist
mailing list