Contact Doug!
Learn About Doug!
View Doug Boude's online resume
updated 11/18/2009

View Doug Boude's profile on LinkedIn
Link to me!

Follow Doug Boude on Twitter
Follow me!

Be Doug's friend on Facebook
Befriend me!
(I promise not to follow you home)
OO Lexicon
Chat with Doug!
Recent Entries
You may also be interested in...
Florida web site design



Czech your Page Rank!
Check Page Rank of any web site pages instantly:
This free page rank checking tool is powered by Page Rank Checker service
Surf's Up!
Visit Egosurf.org and massage YOUR web ego!
My Score: 9,001
Doug's Books

Read (and recommend)

  • Men are from Mars, Women are from Venus
  • The Wisdom of Crowds: Why the Many Are Smarter Than the Few and How Collective Wisdom Shapes Business, Economies, Societies and Nations
  • Blink: The Power of Thinking Without Thinking
  • Head First Design Patterns
  • Transact-SQL Programming
  • What's So Amazing About Grace?
  • Just So Stories (Rudyard Kipling collection)

Reading

  • Prayer: Does it Make Any Difference?
  • Data Mining (Practical Machine Learning Tools and Techniques)
<< May, 2009 >>
SMTWTFS
12
3456789
10111213141516
17181920212223
24252627282930
31
Search Blog

Recent Comments
Categories
Archives
Photo Albums
Funnies (5)
Family (3)
RSS

Powered by
BlogCFM v1.11

12 July 2006
ByRef and ByVal in ColdFusion
Pop Quiz:
Using only your brain (stop laughing!), mentally execute the following code and figure out what values the returned structure will contain:


<cfset listvar = "1,2,3,4">
<cfset stCounter = structNew() >
<cfset stResults = structNew()>
<cfloop list="#listvar#" index="i">
    <cfset stCounter.item = i>
    <cfset stResults[i] = stCounter>
</cfloop>

Choices:
quiz choices
Early yesterday morning my choice would have been ‘B’. Late yesterday afternoon the answer was an obvious “D”. The reason is found in a little thing I like to call “ByRef” (Actually, the whole programming world calls it ByRef). ByRef stands for ‘By Reference’, and it is the polar opposite of ByVal (By Value).

Both of these terms refer to the way in which a value is passed along. You can equate it to the scenario of me giving you $100. If I hand you the cash, I have just given it to you “By Value”; you have it, it’s in your hand. If on the other hand I would have given you a check for $100, then I gave you the money “By Reference”; you don’t really have the money but you’re holding something that points to it.

ColdFusion by default passes structures by reference when you use them as a value. For instance, in the example of

<cfset thisVar = stResults>

(where stResults is a structure), my thisVar variable doesn’t actually have the stResults structure, but merely a pointer to the actual stResults. What this means is that if I modify stResults at some other place in my code and then go back to thisVar to get its value, thisVar’s value would also reflect the same changes. That’s really cool if you planned on things happening that way; not cool if you didn’t.

So how do we pass a structure by value and not by reference? Use the StructCopy function. Using the same example, if I set thisVar with

<cfset thisVar = StructCopy(stResults)>

then modifying the stResults structure afterwards will not reflect within thisVar’s value. StructCopy then is the equivalent of taking a snapshot of the structure, capturing it as it is at that point in time. Consider the following illustration:
byref byval example

If we want the pop quiz code at the beginning of this article to actually produce the answer ‘B’, then we need to make one small modification to it, like so:
<cfset listvar = "1,2,3,4">
<cfset stCounter = structNew() >
<cfset stResults = structNew()>
<cfloop list="#listvar#" index="i">
    <cfset stCounter.item = i>
    <cfset stResults[i] = StructCopy(stCounter)>
</cfloop>

So, boys and girls, that is the difference between ByRef and ByVal. Not typically something we have to ever specify in CF, but DEFINATELY something we have to be aware of since it affects our code (and causes us unnecessary amounts of troubleshooting time).



Posted by dougboude at 4:59 PM | PRINT THIS POST! |Link | 1 comment
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: ByRef and ByVal in ColdFusion
Although structCopy() is limited specifically to structures and is not a deep copy, so if you structCopy() a struct with other structs in it, you'll get a copy of the parent containing a pointer (ref) to the sub-structs. ;) If you need to copy everything (irrespective of whether or not it's a structure), then you have to use duplicate() instead.
Posted by ike on December 4, 2008 at 6:10 PM

Name:   Required
Email:   Required your email address will not be publicly displayed.

Want to receive notifications when new comments are added? Login/Register for an account.

Time to take the Turing Test!!!

7 plus 18 equals
Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!