Collection Inside Collection
Hello, I'd like to know if it is possible and how it can be done to
make collections with inside a class that contains another collection.
I tried it: when I es. add an item to the Inside collection, VB says
Method or data member not found.
Calling Collection1 the main and Collection2 the inside, I wrote:
Coll1.add (...args..., Coll2)
Coll1(1).Coll2(1).Property... ... Method or data member not found
I'm a self-learning (I'm not sure if this is the right way to say) so I'm not sure what is the problem.
Any chance to do this?
Thanks, Gibbofresco
Re: Collection Inside Collection
Welcome to the forums. :wave:
Code aside, what are you trying to accomplish?
1 Attachment(s)
Re: Collection Inside Collection
I think what you are trying to accomplish is a Colection CLass, if so take a look at the attachment.
HOpe this helps!!
:wave: :thumb: :wave:
Re: Collection Inside Collection
Works for me like this:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim MainCol As New Collection
Dim TmpCol As Collection ' Note, I don't use the "New" keyword
Dim K As Long, Q As Long
For K = 1 To 10
Set TmpCol = New Collection ' make a new collection
For Q = 1 To 10 ' get 10 random numbers
TmpCol.Add 100 * Rnd
Next Q
MainCol.Add TmpCol ' add this collection to the main collection
Set TmpCol = Nothing
Next K
' read data in the first collection
For Q = 1 To 10
Debug.Print Q, MainCol.Item(1).Item(Q)
Next Q
End Sub
Re: Collection Inside Collection
Where do you actually add a person to the persons class?
Re: Collection Inside Collection
In the form load event
VB Code:
'new instant of person
Set m_objPerson = New clsPerson
With m_objPerson
.Age = 30
.Name = "A Person"
.Sex = "Male"
End With
m_colPerson.Add m_objPerson
Hope this helps!!
:wave: :thumb: :wave:
Re: Collection Inside Collection
Quote:
Originally Posted by gibbofresco
Coll1(1).Coll2(1).Property... ... Method or data member not found
You can't call the collection like this, as Coll2 is not a method or property of Coll1. Coll1(1). is going to return a collection item, Coll2, that can be accessed with it's methods/properties. You just need to keep track of what get referenced after each dot in your object call. The example below explains better than I can:
VB Code:
Private Sub ColCol()
Dim Col1 As Collection
Dim Col2 As Collection
Set Col1 = New Collection
Set Col2 = New Collection
Call Col1.Add(1, "One")
Call Col1.Add(2, "Two")
Call Col1.Add(3, "Three")
Call Col2.Add(Col1, "Col1")
MsgBox (Col2.Item("Col1").Item("One"))
Set Col2 = Nothing
Set Col1 = Nothing
End Sub
Re: Collection Inside Collection
Bombdrop,
Thanks for the sample code. I have two questions.
I thought that the class_terminate would be called when the object is set to nothing. However, when I made my own,
Public sub destroy()
Set m_colPersons = Nothing
End Sub
and called it from a new button on the form: The msgbox that I put into your terminate event did not display until I closed the program!
So, is the idea that if this form1 was one of many forms in a project and it gets unloaded, setting the object to nothing in the terminate event makes sure that the memory is freed up? Is this because the class_terminate is responding to a message from the form?
Second question: Why are we not setting all of the Person objects in the collection to Nothing as we Terminate the collection?
Thanks,
-MagicT