In my project I have various classes... call them A, B, C etc...

For class A I needed the ability to store them in a collection... so I made this class (CDocument is A):


VB Code:
  1. Public Class CDocumentCollection : Inherits CollectionBase
  2.  
  3. Public Sub Add(ByVal item As CDocument)
  4.             Me.List.Add(item)
  5.         End Sub
  6.         Public ReadOnly Property Item(ByVal index As Integer) As CDocument
  7.             Get
  8.                 Return DirectCast(Me.List.Item(index), CDocument)
  9.             End Get
  10.  
  11.         End Property
  12.  
  13.         Public WriteOnly Property Item(ByVal index As Integer, ByVal Document As CDocument) As CDocument
  14.             Set(ByVal Value As CDocument)
  15.                 Me.List.Item(index) = Document
  16.             End Set
  17.         End Property
  18.  
  19.     End Class
Now let's say I need to do the same thing for B... Is the best way to do that is to create another class BCollection and define all the methods like I did above (add, item) but using the type B instead of type A(CDocument in the example code).

I feel like something stinks in my code here...

I feel like I should only need one collection class.. but since the objects to be stored in the collections have different parents... I can't do it with a base class as collection parameter...

Like if I had A-Z objecs.. all inherited from TheGod class.. then I could make a collection class like above using TheGod as parameter type... then all siblings should be accepted without any type conversions... polymorphism...


That is also the reason I made the class above in the first place.. I want it type safe! But is that possible to acieve WITHOUT for objects A-Z create ACollection-ZCollection?



kind regards
Henrik - pposting yet another bizarre OO question...