Results 1 to 7 of 7

Thread: Object Oriented Help With Collection

  1. #1

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Object Oriented Help With Collection

    Hi all.

    I have an object, say Beer. I have two sub classes that inherit from beer, LiteBeer and HeavyBeer.

    I have a BeerCollection class that I want to use to hold collections of LiteBeer and HeavyBeer. This works fine, as the add method in the collection adds the item as Beer, but I can still access the child properties if cast it back.

    The problem is this: I want the Item method in the collection to return the object as the specific type, not just as a Beer object (code below), so that the object is ready to be used.

    VB Code:
    1. Public ReadOnly Property Item(ByVal index As Integer) As Beer

    I want something like:
    VB Code:
    1. Public ReadOnly Property Item(ByVal index As Integer) As Me.List.Item(0).GetType()
    but obviously you can't do this. Is there any way to accomplish this with out making a separate collection for LiteBeer and HeavyBeer?

    Thanks

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Since the collection class is generic, it looks like you will have to cast.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I'd suggest you just make three different Item properties:
    VB Code:
    1. Public ReadOnly Property Item(ByVal index As Integer) As Beer
    2. Public ReadOnly Property LiteItem(ByVal index As Integer) As LiteBeer
    3. Public ReadOnly Property HeavyItem(ByVal index As Integer) As HeavyBeer

  4. #4

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    My function to add only allows the same type of objects to be added. So my collection will only ever contain one type, not a mixture. So if I have 3 different item properties, then I guess I would throw an error when the other ones are used. For example, If my collection is of LiteBeer objects, then the HeavyItem property would throw an error?

    Is that commonly done? Why make it available if it can't be used or just throws an error?

    I guess this is better then making separate collections though. Hmmm.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    No I don't think its commonly done. People would commonly use casting which is what you said you didn't want to have to do. And yes retieving a HeavyBeer with a LiteBeer index would give you an InvalidCast error.

  6. #6

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    I think I will create a base collection Beer (abstract item method) and then create a LitBeer collection that inherits Beer collection. Then all I have to do is implement the Item property.

    I think that is the best solution - the UI developer doesn't have to worry about casting.

  7. #7

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Thanks for your help, and if any other ideas come to mind, feel free to post!

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