VBA Class() object as property of another class VBA Class() object as property of another class vba vba

VBA Class() object as property of another class


When you myQuotes.Quote(3) you call Property Get Quote which has an issue.

Your internal array of Class2 is not instantiated so pQuote(Index) refers to an array element of Nothing, when you then myQuotes.Quote(3).OTC = you try to assign to Nothing which fails.

You need to make sure pQuote(Index) is instanced; you can do this on demand:

Public Property Get Quote(Index As Integer) As Class2   If (pQuote(Index) Is Nothing) Then Set pQuote(Index) = New Class2   Set Quote = pQuote(Index)End Property

(Note the required Set)

Or by adding an intitialisation routine to Class1:

Private Sub Class_Initialize()    Dim Index As Long    For Index = 0 To UBound(pQuote)         Set pQuote(Index) = New Class2    NextEnd Sub


You need to set them as New Class2 in Class1:

For intI = LBOUND(pQuote) to UBOUND(pQuote)    Set pQuote(intI) =  New Class2Next IntI

Just as you do with Class1 in your final script.


Maybe it should be

Public Property Let Quote(Index As Integer, cQuote As Class2)    Set pQuote(Index) = cQuoteEnd Property