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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why do my scripting.dictionary values change?

Status
Not open for further replies.

shaoxk66

Technical User
Jan 25, 2007
9
CA
Here is a sample code. when i passed the "firstdict" to my second sub, and added another element to "mydict". i want the element to be added in the second sub only to "mydict" not "firstdict". but the element is actually added to both of them. how to fix it?
thanks

Sub dict()
Dim firstdict As New Scripting.Dictionary
firstdict.Add "1/1/1981", 44
firstdict.Add "1/2/1981", 22
changedict firstdict
End Sub
Sub changedict(ByVal firstdict As Scripting.Dictionary)
Dim mydict As Scripting.Dictionary
Set mydict = firstdict
mydict.Add "1/3/1982", 11
'then want to play with mydict, leave "firstdict" no change
End Sub
 
The Set statement sets a reference to an object. In your case Set mydict sets a reference to firstdict. Whatever you change in 'mydict' will be changed in 'firstdict'.
 
Code:
Dim mydict As Scripting.Dictionary
Set mydict = firstdict
This is setting mydict AS firstdict, so any Adds will be added to....firstdict. You do not set mydict as new dictionary.

It is doing things exactly as it is supposed to.

Gerry
My paintings and sculpture
 
I think that to create an independent object, you need to create a new object, and copy all it's properties.

[tt]Dim mydict As Scripting.Dictionary
dim l as long
Set mydict = New Scripting.Dictionary

For l = 0 to firstdict.count-1
mydict.add firstdict.keys(l), firstdict.items(l)
next l
mydict.Add "1/3/1982", 11
'then want to play with mydict, leave "firstdict" no change[/tt]

though It'd be interesting to be proven wrong.

Roy-Vidar
 
I agree with Roy. You cannot duplicate Objects unless the object itself allows a Method for it - and that method would (behind the scenes) do what Roy's code does.

firstdict that you pass to changedict is a pointer. Passing it ByVal passes a copy of the pointer - but it still points to the same object.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
CBA is right, and Roy's solution should work.

A Scripting.Dictionary is an object, so firstDict (variable) is a reference that points to it. So when you call your changeDict sub, it copies the reference to a new variable. The ByVal is irrelevant here, as it only copies the reference, not the object it points to. So now you have two pointers pointing at the same object. Regardless of which one you use, the changes get made to the same object.

You might think this is a fault with VBA. It's not. Consider that values in a dictionary can be other objects (other dictionaries, even). How deep would the copy go? How long would it take each time you called the Sub to copy an entire graph of objects?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks for all you guys. i used Roy's way to do, and it works perfect!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top