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!

Returned Array scope

Status
Not open for further replies.

lijil

Programmer
Jun 9, 2004
16
CA
Is it valid in VBScript to return a reference to an Array created within a Function? This is valid in Javascript, how does VBScript compare? If the answer is no it is not valid, how would one return a deep Array of unknown structure?
 
Here is some code to demonstrate what I think you are asking:
Code:
Option Explicit

Dim myArr
Dim j

myArr = GetMyArray(10)
For j = 0 To UBound(myArr)
	WScript.Echo myArr(j)
Next

Function GetMyArray(nSize)
	Dim arrWorking()
	Dim i
	
	For i = 0 To nSize
		ReDim Preserve arrWorking(i)
		arrWorking(i) = Int(100 * Rnd(1))
	Next
	
	GetMyArray = arrWorking
End Function

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Yes you have declared the arrWorking Array inside the GetMyArray Function and are then returning arrWorking. So myArr does indeed hold a valid reference to the Array created by the function? (i.e. arrWorking is not destroyed when GetMyArray exits?)

And likewise, if arrWorking was populated with Array's instead of Int's those arrays would also be valid once GetMyArray exists?

Thanks
 
I think the answer to both questions is yes.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
ArrWorking is indeed deallocated when the scope of GetMyArray() is exited. A copy of ArrWorking is returned as the value of GetMyArray().

Thus the first answer is "no" even though for passive items like arrays or scalar data it doesn't really matter. If ArrWorking was an object reference or an array of same you should still be ok since assigning the "value" of ArrWorking to GetMyArray() should increment the reference count of any referenced object(s).

Where this fact matters is a case where ArrWorking is altered after GetMyArray() is assigned its value. The copy will not be updated.

Though of course if the alterations take the form of fiddling with referenced objects, the objects are altered and not the reference copy/copies in either Variant. So once again from a program behavior point of view both copies would appeared "synced."

Returning a variant array of arrays should be fine too, everything gets copied. So the second answer should be "yes."

Of course there are performance implications, so if you have a time-sensitive script you'd want to be judicious in doing such a thing as returning arrays of arrays.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top