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

Copy from one dictionary object into another

Status
Not open for further replies.

markronz

IS-IT--Management
Mar 20, 2007
93
US
Hello,
Hopefully this will be a simple question, but does anyone know how to copy one dictionary into another? I tried this code, but it doesn't work:

Code:
Dim dictionary1
Set dictionary1 = CreateObject("Scripting.Dictionary")

Dim dictionary2
Set dictionary2 = CreateObject("Scripting.Dictionary")

dictionary2.Add("1","Test")

Set dictionary1 = dictionary2

Anyone have any idea?

Thanks!
-Mark
 
Sure, I just mean to make an exact copy of it.
I want to put a bunch of stuff into one dictionary object. Then I want to create a copy of that dictionary object, sort of as a backup, so that I can play with the contents of the original dictionary without any fear.
 
Code:
a=dictionary1.items
b=dictionary1.keys
for i=0 to ubound(a)
  dictionary2.add a(i), b(i)
next
may be the only way

_________________
Bob Rashkin
 
I think that is the only way, as it is not really making a copy, it is creating a new dictionary with the same items.

If you SET one dictionary to another then they are equal. So any changes to the first will also be changed in the second.

faq219-2884

Gerry
My paintings and sculpture
 
If you populate both dictionaries at the same time, you'll have a copy of the dictionary.
Code:
Sub Populate_Dictionary()
   Dim i As Integer
   Dim dictionary1, dictionary2
   
   Set dictionary1 = CreateObject("Scripting.Dictionary")
   Set dictionary2 = CreateObject("Scripting.Dictionary")

   For i = 1 To 10
      dictionary1.Add i, i
      [COLOR=red]dictionary2.Add i, i[/color]
   Next i

   Debug.Print dictionary1.Item(5)
   Debug.Print dictionary2.Item(5)
End Sub
 
That would also do it. The point being is you need to make two distinct objects. Again, using SET for one object to another object completely ties them together.

Set dictionary1 = dictionary2

means dictionary1 EQUALS dictionary2. So if you make changes to dictionary2....dictionary1 still means dictionary2. So, it will have the same changes.

Either make them independently but at the same time, and with the same information (WinblowsME), or create the second with the items of the first (Bong), before you make changes.

Excellent alternatives given.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top