Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Returning an Array

Participant ,
Jul 27, 2009 Jul 27, 2009

I have a function that is returning an array that is a local variable inside the component... i beleive an array is considered a complex data type so does this mean its going to pass a referenece rather than a copy?

<cfcomponent>

     <cfset variables.anArray = NewArray(1) />

     <cffunction name="getList" returntype="array">

          <cfreturn variables.anArray />

     </cffunction>

</cfcomponent>

725
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Jul 27, 2009 Jul 27, 2009

Update:  Correction:  I forgot that arrays are an exception.

http://www.coldfusionjedi.com/index.cfm/2009/5/1/ColdFusion-and-Pass-by-Reference-versus-Value

But confusingly, you can still modify the underlying array under some circumstances. For example:

<!--- this would modify the underlying array --->

<cfset arrayAppend( application.myComponent.getList(), "new value")) >
<cfdump var="#application.myComponent.getList()#" label="getList() contents">

<!--- this WILL NOT change the array inside the compo

...
Translate
Valorous Hero ,
Jul 27, 2009 Jul 27, 2009

Test

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 27, 2009 Jul 27, 2009

No, it should pass a copy.  Passing a reference would be dangerous when you consider it would allow you to alter private variables outside of the component (which would completely defeat the purpose of using variable scope to begin with).  I could be completely off base on this, but I believe the only time coldfusion passes a reference rather than a copy is when you are passing an object.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 27, 2009 Jul 27, 2009

That was my thought as well, I just wanted a second opinion...  anyone know if this is incorrect?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jul 27, 2009 Jul 27, 2009
LATEST

Update:  Correction:  I forgot that arrays are an exception.

http://www.coldfusionjedi.com/index.cfm/2009/5/1/ColdFusion-and-Pass-by-Reference-versus-Value

But confusingly, you can still modify the underlying array under some circumstances. For example:

<!--- this would modify the underlying array --->

<cfset arrayAppend( application.myComponent.getList(), "new value")) >
<cfdump var="#application.myComponent.getList()#" label="getList() contents">

<!--- this WILL NOT change the array inside the component --->
<cfset copyOfArray = application.myComponent.getList() />
<cfset arrayAppend( copyOfArray, "new value")) >

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources