"Betina Andersen" <by*@invalid.comwrote in message
news:O1**************@TK2MSFTNGP04.phx.gbl...
I have a dictionary object, then I create a new dictionary object and sets
it equal to my original, then I pass the new dictionary object to a
function
that changes some of my values - but then my original dictionary also gets
changed and that was not the intention, can someone explain to me why it
behaves that way and how do I avoid it, så I van have different dictionary
objects?
My very old copy of MSDN says...
"Generally, when you use Set to assign an object reference to a variable, no
copy of the object is created for that variable. Instead, a reference to the
object is created. More than one object variable can refer to the same
object. Because these variables are references to (rather than copies of)
the object, any change in the object is reflected in all variables that
refer to it."
I would guess that the following occurs...
dim x
dim z
set x = CreateObject("Scripting.Dictionary")
'x contains (for instance) the number 536474
'which is a memory address that points to the dictionary object
set z = CreateObject("Scripting.Dictionary")
'z contains a pointer to a different object e.g. the memory address 73462
set z = x
'z now contains 536474, and so points to the original object - same as x
I think you need something like this...
set z = copydict(x)
function copydict(x)
dim j, k, i, z
set z = CreateObject("Scripting.Dictionary")
k = x.Keys
i = x.Items
for j = 0 to x.Count - 1
z.Add k(j), i(j)
next
set copydict = z
end function
--
roger