new array

I have an issue where I have a dictionary of arrays of objects.

dim o as new objectA dim d as new dictionary // In reality this is a class property. dim ar(-1) as objectB dim b as new objectB ... if d.HasKey(o) then ar = d.Value(o) else redim ar(-1) // This should be a new array d.Value(o) = ar end if ar.append(b)

Not sure why but the array is always -1 in length. I suspect because it is always a reference to the same array.
Can anyone see any reason why these arrays are being clobbered?

That code looks ok. Do you do anything with ar() in the elipsis (the code you didn’t provide)? If you truly want a new array here, use this:

if d.HasKey(o) then
   ar = d.Value(o)
else
   dim newar(-1) as objectB   // This is a new array
   ar = newar   // ar no longer points to the previous array
   d.Value(o) = ar
end if

But unless the above code is inside a loop, your problem lies elsewhere. (If it is in a loop, the above should fix it.)

Hi Tim, Thanks for the reply. This is indeed in a loop.
I don’t really do anything other than just a loop that builds the dictionary and appends other objects to the arrays…

when I look at debugger
dictionary
1 objecta, objectb(-1)
2 objecta, objectb(-1)
3 objecta, objectb(-1)
4 objecta, objectb(-1)

As Tim pointed out, redim does not create a new array. However, putting the declaration within a block, like an IF block, will create a new array each time.