• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

What does Strong Typed mean for VB.NET CollectionBase

Hi experts, I'm just wondering what does  Strong Typed mean for .NET CollectionBase Class

is it possible to hold both base and its derived class in a collectionbase?

For example, I have a base class and a class derived from the base class

public class BaseClass
    protected member1 as string

    public overridable sub mySub()
       'do something for base class
    End Sub
End Class

Public Class DerivedClass
   Inherits BaseClass
   
   Public override Sub mySub()
       'Do somthing and override mySub in base class
   End Sub
End Class

Now I define a baseclass collection

Public Class BaseClassCollection
   Inherits CollectionBase
   .....
End Class


and suppose I define an instance of BaseClassCollection

Dim myCollection as BaseClassCollection

and I put a few BaseClass objects and DerivedClass into myCollection. and suppose myCollection.Item(1) is of type DerivedClass

My question is, if I call the member function myCollection.Item(1).mySub(), is baseClass.mySub() invoked or DerivedClass.mySub() invoked?

Thanks for your help


0
caibeier
Asked:
caibeier
1 Solution
 
jrschererCommented:
All depends what type you define for the Item of the BaseClassCollection. If you define it as DerivedClass then the properties of the DerivedClass will be used.
A class inherited from CollectionBase is strong typed. This means only one type can be stored, and you have to define this type through the Item property yourself.

Default Property Item(ByVal index As Integer) As DerivedClass
    Get
        Return CType(InnerList.Item(index), DerivedClass)
    End Get
    Set(ByVal Value As DerivedClass)
        InnerList.Item(index) = Value
    End Set
End Property


Jack.net
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now