2013-07-05 18 views
6

Próbuję utworzyć klasę do przechowywania zmiennej liczby elementów (które same są obiektem innej klasy).Obiekt klasy VBA() jako własność innej klasy

Więc mam klasa 2:

 
' Class 2 contain each individual quote elements (OTC and MRC) 

Private pOTC As String 
Private pMRC As String 
Public Property Get OTC() As String 
    OTC = pOTC 
End Property 
Public Property Let OTC(Value As String) 
    pOTC = Value 
End Property 

Public Property Get MRC() As String 
    MRC = pMRC 
End Property 
Public Property Let MRC(Value As String) 
    pMRC = Value 
End Property 

Następnie klasa 1 zawiera tablicę Klasa 2:

 
Private pCurr As String 
Private pQuote(20) As Class2 

Public Property Get Curr() As String 
    Curr = pCurr 
End Property 
Public Property Let Curr(Value As String) 
    pCurr = Value 
End Property 

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

Public Property Get Quote(Index As Integer) As Class2 
    Quote = pQuote(Index) 
End Property 

i co chciałbym zrobić coś takiego jak:

 
Dim myQuotes As Class1 
Set myQuotes = New Class1 

myQuotes.Curr = "GBP" 
myQuotes.Quote(3).OTC = "1200" 

Ustawienie pierwszej linii myQuotes.Curr nie jest problemem, jednak gdy próbuję ustawić wartość w tablicy, następna linia błędów h Run-time obiektu lub zmienna 91 ze zmienną bloku nie ustawić

Wszelkie wskazówki co do tego, co robię źle i jak mogę ustawić wartości dla elementów w tablicy klasy?

Z góry dziękuję!

+0

oprócz rozwiązania problemu, który jest poniżej, dzięki Alex K., czy mogę po prostu zapytać (ciekawości), dlaczego robisz to tak, jak jesteś, zamiast korzystać z kolekcji cytatów? –

Odpowiedz

4

Po zgłoszeniu myQuotes.Quote(3) dzwonisz pod numer Property Get Quote, który ma problem.

Twoja wewnętrzna tablica Class2 nie jest tworzony tak pQuote(Index) odnosi się do elementu tablicy Nothing, po czym myQuotes.Quote(3).OTC = spróbować przypisać do Nothing który kończy się niepowodzeniem.

Musisz upewnić się, że wystąpiła instancja pQuote(Index); można to zrobić na żądanie:

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 

(Uwaga wymagana Set)

lub dodając rutynowych intitialisation do Class1:

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

Dzięki! To się udało!! Zauważyłem również inny błąd w klasie 1, ponieważ powinien przeczytać ** Set Quote = pQuote (Index) **: 'Public Property Get Quote (Index Integer) As Class2 Set Quote = pQuote (Index) Właściwość końcowa' – freudian

0

Może powinno być

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

Musisz ustawić je jako nowe Class2 w Class1:

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

Tylko jak to zrobić z Class1 w swoim ostatecznym scenariuszu.