|
-
Jul 7th, 2012, 07:49 PM
#1
Thread Starter
New Member
Storing and retrieving Dictionary objects in a Dictionary object
I've had trouble trying to locate info online for how to store/retrieve objects from a Dictionary object. Particularly, I wanted to store multiple Dictionary objects (and retrieve them) from a single Dictionary object. Well, after searching a great deal online and finding nothing, I did some experimenting and found the answer myself.
Why would I want to it this way? Well, I'm currently needing a fast, easy way to implement a B+Tree implementation of SQLite using VB6 and VB.NET, as well as Golds Parser (goldparser.org) for the SQL language validation code-generation. Anyhow, all of that aside, here's the Dictionary code that works (after finding about 200 other ways in which it won't work):
Public Sub test2()
Dim dict1 As New Dictionary
Dim dict2 As New Dictionary
Dim varData As Variant
dict2.Add "val1", "frank"
dict2.Add "val2", "james"
dict2.Add "val3", "susan"
dict1.Add 0, dict2
Set dict2 = Nothing
Set dict2 = New Dictionary
dict2.Add "val1", "human"
dict2.Add "val2", "cyborg"
dict2.Add "val3", "animal"
dict1.Add 1, dict2
Set dict2 = Nothing
Set dict2 = New Dictionary
Debug.Print dict1.Count
For Each varData In dict1.Items
Debug.Print varData("val1"), varData("val2"), varData("val3")
Next varData
End Sub
The trick is basically to just destroy and re-instantiate the 2nd Dictionary (which I use multiple times in the real code) so that multiple Dictionary lists (dict2) can be stored, one per Item in the main Dictionary object (dict1).
I originally tried just doing a dict2.RemoveAll, but if you try storing 3 dict2 objects in dict1, you'll have 3 items in dict1, but every one of them will be the contents of the last item you put it in. Not the desired outcome. However, destroying the dict2 object and re-instantiating it (Set dict2=Nothing | Set dict2 = New Dictionary) does the trick. Each instance of dict2 is stored (and retrieved) correctly and successfully.
Happy programming!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|