Results 1 to 8 of 8

Thread: Collection Inside Collection

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    19

    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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Collection Inside Collection

    Welcome to the forums.

    Code aside, what are you trying to accomplish?

  3. #3
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    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!!
    Attached Files Attached Files

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Collection Inside Collection

    Works for me like this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim MainCol As New Collection
    5.     Dim TmpCol As Collection ' Note, I don't use the "New" keyword
    6.    
    7.     Dim K As Long, Q As Long
    8.    
    9.     For K = 1 To 10
    10.         Set TmpCol = New Collection ' make a new collection
    11.        
    12.         For Q = 1 To 10 ' get 10 random numbers
    13.             TmpCol.Add 100 * Rnd
    14.         Next Q
    15.        
    16.         MainCol.Add TmpCol ' add this collection to the main collection
    17.         Set TmpCol = Nothing
    18.     Next K
    19.    
    20.     ' read data in the first collection
    21.     For Q = 1 To 10
    22.         Debug.Print Q, MainCol.Item(1).Item(Q)
    23.     Next Q
    24. End Sub

  5. #5
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Collection Inside Collection

    Where do you actually add a person to the persons class?
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  6. #6
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Re: Collection Inside Collection

    In the form load event

    VB Code:
    1. 'new instant of person
    2.     Set m_objPerson = New clsPerson
    3.     With m_objPerson
    4.         .Age = 30
    5.         .Name = "A Person"
    6.         .Sex = "Male"
    7.     End With
    8.  
    9.     m_colPerson.Add m_objPerson

    Hope this helps!!


  7. #7
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    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:
    1. Private Sub ColCol()
    2.  
    3.     Dim Col1 As Collection
    4.     Dim Col2 As Collection
    5.    
    6.     Set Col1 = New Collection
    7.     Set Col2 = New Collection
    8.    
    9.     Call Col1.Add(1, "One")
    10.     Call Col1.Add(2, "Two")
    11.     Call Col1.Add(3, "Three")
    12.  
    13.     Call Col2.Add(Col1, "Col1")
    14.  
    15.     MsgBox (Col2.Item("Col1").Item("One"))
    16.  
    17.     Set Col2 = Nothing
    18.     Set Col1 = Nothing
    19.  
    20. End Sub
    Last edited by Comintern; Oct 28th, 2005 at 10:45 AM. Reason: clarify language.

  8. #8
    Member
    Join Date
    Oct 2005
    Posts
    38

    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
    MagicT

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width