+ Reply to Thread
Results 1 to 2 of 2

Collection of Class Modules

Hybrid View

wildjester Collection of Class Modules 07-10-2009, 01:03 PM
Andy Pope Re: Collection of Class... 07-11-2009, 09:26 AM
  1. #1
    Registered User
    Join Date
    07-10-2009
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Collection of Class Modules

    Hi all.

    I am trying to create a collection of class modules. (Originally, I wanted to have a collection of user defined types. But after hours of head banging realized VBA doesn't let you do that)

    I think that the problem I am having has to do with the fact that the class i want to add to the collection has two class module's in it. [ See Below ]


    ' This is the class module that I want to add to a collection,
    ' Lets call it cMaster:
    
    Private pA As cAlpha
    Private pB As cBeta
    
    ' Gets and lets here... 
    Public Property Get A() As cAlpha
    Public Property Let A( a_ As cAlpha )
    Public Property Get B() As cBeta
    Public Property Let B( b_ As cBeta )
    Note: cAlpha and cBeta are each Class Modules that I created.


    One question, do I need to call New on cAlpha and cBeta members?

    Sub Init()
        Set pA = New cAlpha
        Set pB = New cBeta
    End Sub

    Because right now I do:

    Dim myMaster As cMaster
    Set myMaster = New cMaster
    Call myMaster.Init
    Now, I would LIKE to do the following:

    Dim Arhs As cAlpha
    Set Arhs = New cAlpha
    Arhs.val = something
    
    Dim Brhs As cBeta
    Set Brhs = New cBeta
    Brhs.val = something
    
    myMaster.A = Arhs
    myMaster.B = Brhs
    
    ' Create a collection to store all the masters
    Dim AllMasters As Collection
    Set AllMasters = New Collection
    
    
    ' BUT, ERROR EXECUTING LINE BELOW:
    
    AllMasters.Add myMaster
    The error is as follows:

    Run-time error '91':

    Object variable or With block variable not set


    BUT WHERE?


    Thanks to anyone that can help!
    Last edited by wildjester; 07-15-2009 at 12:53 PM.

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

    Re: Collection of Class Modules

    This is how I go about doing collections of classes.

    CAlpha
    Public Val As String
    CBeta
    Public Val As String
    CMaster
    Private pA As CAlpha
    Private pB As CBeta
    
    Public Property Set A(a_ As CAlpha)
        Set pA = a_
    End Property
    
    Public Property Get A() As CAlpha
        Set A = pA
    End Property
    
    Public Property Set B(b_ As CBeta)
        Set pB = b_
    End Property
    
    Public Property Get B() As CBeta
        Set B = pB
    End Property
    
    Private Sub Class_Initialize()
        
        Set pA = New CAlpha
        Set pB = New CBeta
        
    End Sub
    CMasters
    Private m_colMasters As Collection
    Public Function Add() As CMaster
    
        Dim clsMaster As CMaster
        
        Set clsMaster = New CMaster
        m_colMasters.Add clsMaster, CStr(m_colMasters.Count + 1)
        Set Add = m_colMasters(m_colMasters.Count)
        
    End Function
    
    Public Function Master(Index As Variant) As CMaster
    
        On Error Resume Next
        Set Master = m_colMasters(Index)
        Exit Function
        
    End Function
    Public Function Masters() As Collection
        Set Masters = m_colMasters
    End Function
    
    
    Private Sub Class_Initialize()
    
        Set m_colMasters = New Collection
        
    End Sub
    Test module to demostrate uses
    Sub Test()
    
        Dim clsMasters As CMasters
        Dim clsMaster As CMaster
        Dim clsAlpha As CAlpha
        Dim clsBeta As CBeta
        
        Set clsMasters = New CMasters
        
        Set clsMaster = clsMasters.Add
        clsMaster.A.Val = "Alpha"
        clsMaster.B.Val = "Beta"
        
        Set clsMaster = clsMasters.Add
        clsMaster.A.Val = "Gamma"
        clsMaster.B.Val = "Delta"
        
        For Each clsMaster In clsMasters.Masters
            Debug.Print "A="; clsMaster.A.Val, "B="; clsMaster.B.Val
        Next
        
        Set clsAlpha = clsMasters.Master(2).A
        Set clsBeta = clsMasters.Master(1).B
        
        Debug.Print "There are "; clsMasters.Masters.Count; " in Masters Collection"
        Debug.Print "A="; clsAlpha.Val, "B="; clsBeta.Val
        
        Set clsMasters = Nothing
        
    End Sub
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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