Actually, how to do this is undocumented, but possible. If you want to implement syntax for each syntax for a collection, you can do the following:
Option Compare Database Option Explicit Public colT As New Collection Public Function NewEnum() As IUnknown Set NewEnum = colT.[_NewEnum] End Function Public Property Get NextItem() As IUnknown Attribute NextItem.VB_UserMemId = -4 Attribute NextItem.VB_MemberFlags = "40" Set NextItem = colT.[_NewEnum] End Property
Note the attribute settings above. You should use SaveAsText and edit the code as indicated above in notepad. Then you re-import the code using loadfromText on the debug command line. Once you do this, you can go:
Dim n As clstest1 Dim v As Variant Set n = New clstest1 [ code here that adds to collection] For Each v In n Debug.Print v Next
And if you do not want to use for each ... for the collection, you can / can also set the default property for the class by going:
Public Property Get Item(Optional ndx As Integer = 1) As Variant Attribute Item.VB_UserMemId = 0 Select Case ndx Case 1: Item = Me.s1 Case 2: Item = Me.s2 Case 3: Item = Me.s3 End Select End Property Public Property Get count() As Integer count = 3 End Property
Then you can go:
Dim n As clstest1 Dim i As Integer Set n = New clstest1 For i = 1 To n.count Debug.Print n(i) Next
However, I do not know how you can automatically add each method / member of the class to the built-in automatic assembly of objects (there is no way to serialize this with compiler parameters, but I saw the code with each procedure with the attribute Item.VB_UserMemId = 1, then 2, then 3 ) Maybe someone with a lot of knowledge can jump over).
However, as shown above, you can implement for..each for collections. And you can implement an index for each of the properties / methods if you create your own element property. And, as shown above, you can even set this property of the element that you create as the default value. I added "optional" and therefore even:
debug.print n
Will work, or
debug.print n.Item(1)