访问项目集合中的属性

我有一个类的集合。 虽然我似乎无法访问我的课程的属性。 这是我能做的吗?

这是我的课clsProj:

Option Explicit Private pValue As String Public Property Get Value() As String Value = pValue End Property Public Property Let Value(tempv As String) pValue = tempv End Property 

而我的子:

 Sub testtt() Set cp = New Collection cp.Add clsProj, "AAA" cp.Add clsProj, "BBB" cp("AAA").Value = "OK" MsgBox (cp("AAA").Value) End Sub 

总之,我有一个clsProj类,我用string索引(这只是一个testing子)的集合,我想要访问给定的集合项目ex:AAA在这种情况下clsProj的属性。 这里有什么错误? 我似乎无法得到它。

类有点棘手的理解,但是当你做的时候,他们真的很有用。 也许这会有所帮助:

 Sub testtt() Dim cp As Collection Set cp = New Collection Dim blabla As clsProj Set blabla = New clsProj Dim blabli As clsProj Set blabli = New clsProj blabla.Value = "OK" blabli.Value = "KO" cp.Add blabla, "AAA" cp.Add blabli, "BBB" MsgBox (cp("AAA").Value) MsgBox (cp("BBB").Value) Set blabla = Nothing Set blabli = Nothing End Sub 

编辑:混合CollectionClassFor...Next循环:

 Sub testtt() Dim cp As Collection Set cp = New Collection Dim blabla As clsProj Dim i As Integer For i = 1 To 10 Set blabla = New clsProj '"OK" value + a special character from ASCII table blabla.Value = "OK " & Chr(32 + i) cp.Add blabla, CStr("AAA" & i) Set blabla = Nothing Next i 'Test calling collection by key MsgBox cp("AAA5").Value 'Test calling collection by item number and print it in '"Immediate" window (ctrl+g to show that window from VBA editor) For i = 1 To cp.Count Debug.Print cp(i).Value Next i End Sub