1

I have 2 arrays

array1 = Array("elem1", "elem2", "elem3")
array2 = Array("item1", "item2", "item3")

I then select one of the arrays

Randomize
dim refArray
if Rnd < 0.5 then
    refArray = array1
else
    refArray = array2
end if

And I make changes to its elements

refArray(0) = "foo"
refArray(1) = "bar"

Say Rnd is less than 0.5 and refArray = array1 executes. I would like that both array1 and refArray point to the same piece of memory, so when I make changes to refArray they should also be visible in array1.

After the code executes I would expect the contents of array1 to be: "foo", "bar", "elem3". But instead it remains unchanged.

The problem I am having is that vbs does not pass a reference to array1 or array2, but instead it duplicates it to a new array refArray, which gets the changes and leaves array 1 and 2 unchanged.

How can I get a reference to the array and have the changes made to refArray apply to the referenced object (normal Java/C usage)?

Thanks.

2 Answers 2

2

The only way to get a reference to a native VBScript array is a Sub/Function call:

>> Sub assignArray(a, i, e)
>>   a(i) = e
>> End Sub
>> array1 = Array("elem1", "elem2", "elem3")
>> array2 = Array("item1", "item2", "item3")
>> WScript.Echo "array1", Join(array1), "array2", Join(array2)
>> assignArray array1, 0, "abra"
>> assignArray array2, 0, "cadabra"
>> WScript.Echo "array1", Join(array1), "array2", Join(array2)
>>
array1 elem1 elem2 elem3 array2 item1 item2 item3
array1 abra elem2 elem3 array2 cadabra item2 item3

If that does not solve your real-world problem - btw: what is your real-word problem? - consider to use objects (Dictionary, System.Collections.ArrayList) instead.

To spell it out:

Array-assignment copies. References to (native) arrays are possible by parameter passing only. As VBscript is neither C nor Java, you'll have to adapt your 'design' to the language - e.g.:

Option Explicit

Sub assignArray(a, i, e)
   a(i) = e
End Sub

Randomize

Dim a1 : a1 = Split("I don't believe this")
Dim a2 : a2 = Split("solves any real-word problem")
WScript.Echo "a1:", Join(a1)
WScript.Echo "a2:", Join(a2)
If Rnd < 0.5 Then
   assignArray a1, 0, "We"
Else
   assignArray a2, 3, "problems"
End If
WScript.Echo "a1:", Join(a1)
WScript.Echo "a2:", Join(a2)

output:

a1: I don't believe this
a2: solves any real-word problem
a1: We don't believe this
a2: solves any real-word problem

a1: I don't believe this
a2: solves any real-word problem
a1: I don't believe this
a2: solves any real-word problems
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the input, but this doesn't help me (except maybe for the suggestion to use something other than arrays). I have made changes to my original question and hope it is clearer.
2

If you need to handle arrays by reference, consider using ArrayList objects instead:

>>> Set a1 = CreateObject("System.Collections.ArrayList")
>>> a1.Add 1
>>> a1.Add 2
>>> a1.Add 3
>>> WScript.Echo "[" & Join(a1.ToArray, ",") & "]"
[1,2,3]
>>> Set a2 = a1
>>> a2(1) = 4
>>> WScript.Echo "[" & Join(a2.ToArray, ",") & "]"
[1,4,3]
>>> WScript.Echo "[" & Join(a1.ToArray, ",") & "]"
[1,4,3]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.