+ Reply to Thread
Results 1 to 4 of 4

Collections within class module collections

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Question Collections within class module collections

    I'm trying to get a handle on establishing collections within collections in a class module database.. I've read up loads of notes on the subject but I'm still missing something..
    Can anyone provide me the simplest possible example of a class module database comprising a class module containing a collection of :-
    1 property
    and
    1 sub collection.

    i know how to set up properties and methods but the correct collections syntax eludes me.
    Appreciate any help/tips advice or examples.... Thanks
    Elegant Simplicity............. Not Always

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Collections within class module collections

    what do you mean by a collection of 1 property?
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Collections within class module collections

    Does this help.

    Module
    Sub Test()
    
        Dim clsPersons As CPersons
        Dim clsPerson As CPerson
        Dim lngIndex As Long
        
        Set clsPersons = New CPersons
        
        clsPersons.Add "Paul"
        clsPersons.Add "John"
        clsPersons.Add "George"
        clsPersons.Add "Ringo"
        
        For Each clsPerson In clsPersons.Persons
            Debug.Print clsPerson.Name
        Next
        
        Debug.Print "There are " & clsPersons.Count; " people"
        Debug.Print "Person 3 is "; clsPersons.Person(3).Name
        
        Set clsPersons = Nothing
        
    End Sub
    Class CPerson
    Option Explicit
    
    Private m_strName As String
    Public Property Let Name(RHS As String)
        m_strName = RHS
    End Property
    
    Public Property Get Name() As String
        Name = m_strName
    End Property
    class CPersons
    Option Explicit
    
    Private m_colPersons As Collection
    Public Function Add(Name As String) As CPerson
        
        Dim clsPerson As CPerson
        
        Set clsPerson = New CPerson
        clsPerson.Name = Name
        m_colPersons.Add clsPerson, Name
        Set Add = clsPerson
        Exit Function
    End Function
    
    Public Function Count() As Long
        Count = m_colPersons.Count
    End Function
    
    Public Function Person(Index As Variant) As CPerson
        On Error Resume Next
        Set Person = m_colPersons(Index)
        Exit Function
    End Function
    
    Public Function Persons() As Collection
        Set Persons = m_colPersons
    End Function
    
    Private Sub Class_Initialize()
        Set m_colPersons = New Collection
    End Sub
    
    Private Sub Class_Terminate()
        Set m_colPersons = Nothing
    End Sub
    Cheers
    Andy
    www.andypope.info

  4. #4
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Collections within class module collections

    Cheers Andy .. I'll plug that in and have a play.

    Joseph, what i meant by "a collection of 1 property?" was a collection and a property.... Bad grammar on my part... apologies.

    ....... Right, playtime

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1