?Using Collection class?

Krikke_vdk

Newcomer
Joined
Sep 24, 2003
Messages
2
Location
Belgium
Hi,

In VB 6 you have the standard forms collection. In VB.NET this collection doesn't exists anymore. So I’m trying to build my own forms collection. I created a base class where all my other forms are based on. In this base class I add the form to a collection when it loads.
This works fine. I have some code in the collection class to add items to the collection and a property Item. This code is similar as in VB6 when you use the class builder (does the class builder exists in VB.NET) But the return type of the property item can not be set to the client class (= the classes the collection class contains). When I set the return type of my class equal to the client class a syntax error appears = "option strict on disallows implicit conversions from System.Object to the client class"

Do any of you have an idea on how to solve this issue?
If more information is required, just ask...

Thanks in advance
 
If you want an old Form Collection:
Visual Basic:
Class FormCollection
      Public Shared Forms As Collection = New Collection()
End Class
In every forms Sub New put:
Visual Basic:
FormCollection.Forms.Add(Me)
Then put the remove code in the Closing Event
 
Not a regular collection

Ok this is indeed a possible way.

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 doesn't 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 couldn't 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)

Hope you better understand my problem now.

Thanks in advance
 
Back
Top