Results 1 to 2 of 2

Thread: Proper inhertance?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Proper inhertance?

    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...

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    If you want different kinds of objects in one collection, then you will need to have some common super class, or interface. But that removes type safety. The only way you are going to get close to type safety and allow multiple classes to use the same collection class would probably be to simulate generics.

    VB Code:
    1. Imports System.Collections
    2.  
    3. Public Class CollectionClass : Inherits CollectionBase
    4.  
    5.     Private _Type As Type
    6.  
    7.     Public Sub New(ByVal T As Type)
    8.         _Type = T
    9.     End Sub
    10.  
    11.     Public Sub Add(ByVal o As Object)
    12.  
    13.         If o.GetType Is _Type Then
    14.             MyBase.List.Add(o)
    15.         Else
    16.             Throw New InvalidOperationException("Object is not of type " & _Type.Name)
    17.         End If
    18.  
    19.     End Sub
    20.  
    21. End Class

    Something like that.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width