
Originally Posted by
YasserKhalil
c(j).Add Key:=strKey, Item:=Array(strKey, New Collection)
If we only have one collection, say :
Dim coll as New Collection
then the syntax to add data to the collection would be :
coll.Add Key:=strKey, Item:=.....
But if we have many collections with uniform structures, we can set array where each of its member is a collection.
It is like you have many variables vs you put them in single array like this :
- Many variables :
Dim i, j, k
i = 1
j = 2
k = 3
- Using array :
Dim arr(1 to 3)
arr(1) = 1
arr(2) = 2
arr(3) = 3
The same thing applied to collection, if you have many collections, your code would be like this :
- Many collections :
Dim FirstColl as New Collection, SecondColl as New Collection, ThirdColl as New Collection
FirstColl.add Key:=... , Item:= ...
SecondColl.add Key:=... , Item:= ...
ThirdColl.add Key:=... , Item:= ...
- Using array :
Dim coll(1 to 3) as New Collection
coll(1).add Key:=... , Item:= ...
coll(2).add Key:=... , Item:= ...
coll(3).add Key:=... , Item:= ...

Originally Posted by
YasserKhalil
c(j)(strKey)(1).Add Empty
I have once answer your question about collection inside collection (nested).
Collection can store any kind of variables, arrays, and classes.
So you can :
coll.add Key:=... , Item:=100 '--> Store simple value
coll.add Key:=... , Item:=Array(1,2,3) '--> Store array
coll.add Key:=... , Item:=New Collection '--> Store "child" collection inside collection (like putting smaller box inside big box)
coll.add Key:=... , Item:=Array(1, New Collection) '--> Store array, where the second member of the array is a collection
If we want to access an array, we will call it through its index, for example :
arr = Array(1, 2, 3)
Debug.Print arr(1)
arr(1) = "Second"
Debug.Print arr(1)
So now we put it all together :
- to access the member of single collection, it would be :
- to access the member of arrayed collection, it would be :
- If the value stored in ".item" is an array, to access the member of this array would be :
v = coll(3)("TheKey")(0)
v = coll(3)("TheKey")(1)
v = coll(3)("TheKey")(2)
where (0), (1), (2), etc, is index of the array, just like "arr = Array(1, 2, 3) --> Debug.print arr(1)"
- So to access the child collection of big the collection, the index to be used is 1 (because it is zero based index, created using "Array(strKey, New Collection)" syntax) :
- To add some data to this child collection, the syntax is :
c(j)(strKey)(1).Add "Something..." 'In this case we ".Add Empty", because we only want to count the number of occurences
Bookmarks