But I had a more advance collection in mind.
In VB6 it is possible to have the collection return a class.
For example
I have 2 classes "Customers" and "Customer"
The class "Customers" contains a collection of "Customer"
The class "Customers"
Private mCol As Collection
Public Function Add(ByVal iID As Integer) As Customer
create a new object
Dim oNewMember As Customer
Check if ID doesnt alread exists if so exit
Set oNewMember = New Customer
oNewMember.ID= iID
Add to collection
mCol.Add oNewMember
return the object created
Set Add = oNewMember
Set oNewMember = Nothing
End Function
Public Property Get Item(vntIndexKey As Variant) As Customer
Set Item = mCol(vntIndexKey)
End Property
Public Property Get Count() As Long
Count = mCol.Count
End Property
Public Sub Remove(vntIndexKey As Variant)
Dim intItemCount As Integer
For intItemCount = vntIndexKey + 1 To mCol.Count
mCol(intItemCount).Index = mCol(intItemCount).Index - 1
Next
mCol.Remove vntIndexKey
End Sub
Private Property Get NewEnum() As IUnknown
Set NewEnum = mCol.[_NewEnum]
End Property
Private Sub Class_Initialize()
Set mCol = New Collection
End Sub
Private Sub Class_Terminate()
Set mCol = Nothing
End Sub
This would be the code in de class "Customer"
Private mvarID as Integer
Public property Let ID (ByVal ID as Integer)
mvarID = ID
end property
Public property Get ID () as Integer
ID = mvarID
end property
Other properties and functions can be added.
When you use the class "Customers" now you can do like this
Customers.Item(1).[Receive all the properties and subs of the class "Customer" Because the return value of the default Item is the class "Customer"
I have search to do the same in .NET but couldnt find it.
I already implemented the System.Collections.IEnumerable
But when I set the return type of my default value Item of the collection to "Customer" he gives the msg error. (the one in my first post)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.