Help with .NET Collections

My .NET collections usually look something like ...

Public Class DogCollection
    Inherits System.Collections.CollectionBase

with typed Add/Remove/Item/Contains methods.


I would like to start creating more versatile collections.

I'd like to be able to ...

>> Access values by index or a string key
>> Custom sorting (not just by key)
>> dim dog1 as dog = dogCollection1("Murphy") or dogCollection1(0) ... how do you make make default methods like these?  for ex, the ListView has an items collection, the ListViewItems of which can be accessed by listView1.Items(displayIndex as integer) instead of listView1.Items.Item(displayIndex as integer) - it's a nifty shortcut, especially for collections.


I'll distrubute points fairly to whoever helps out -- links to good examples are appreciated, too.  Just don't post a link to MSDN's System.Collections page or something and expect points.  Crazy kids :P

Thanks in advance!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Solutions ArchitectCommented:
Making a custom collection is a very wide scenario, I can't just give you an exact solution, you must implement the one that suites best your needs.
What I give you here is a pretty good scenario to get you into it.

This custom collection have the particularity of being able to be serialized (make sure the item is serializable too.)

<Serializable()> Public Class myCollection
    Inherits CollectionBase

    Public Sub New()
        'Use it as you need...
    End Sub

    'Add methods
    Public Function Add(ByVal obj As myCollectionItem) As Integer
        Return Me.List.Add(obj)
    End Function
    Public Function AddRange(ByVal objs() As myCollectionItem) As Integer()
        If objs Is Nothing Then Return Nothing

        Dim retval(objs.Length - 1) As Integer
        For i As Integer = 0 To objs.Length - 1
            retval(i) = Me.Add(objs(i))

        Return retval
    End Function

    'Remove methods
    Public Sub Remove(ByVal obj As myCollectionItem)
    End Sub

    'Default Item method
    Default Public Property Item(ByVal index As Integer) As myCollectionItem
            If (index >= 0) AndAlso (index <= Me.List.Count - 1) Then
                Return Me.List(index)
            End If
        End Get
        Set(ByVal Value As myCollectionItem)
            If (index >= 0) AndAlso (index <= Me.List.Count - 1) Then
                Me.List(index) = Value
            End If
        End Set
    End Property

    'Sort Methods
    Public Sub Sort()
        If Me.List.Count = 0 Then Exit Sub 'Can't sort an itemless collection
        If Not TypeOf Me.List(0) Is IComparer Then Exit Sub 'Can't sort an collection if the items don't implement IComparer

        Me.InnerList.Sort(CType(Me.List(0), IComparer))
    End Sub
    Public Sub Sort(ByVal comparer As IComparer)
    End Sub
    Public Sub Sort(ByVal index As Integer, ByVal count As Integer, ByVal comparer As IComparer)
        Me.InnerList.Sort(index, count, comparer)
    End Sub

End Class

<Serializable()> Public Class myCollectionItem
    Implements IComparer

    '... Add your collection item properties and whatever here ...

    Friend Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare

    End Function
End Class

Alex :p

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alexandre SimõesManager / Solutions ArchitectCommented:
I forgot the Compare code... but as I gave you a generic item, you must make the comparisson code for each item type you need, and return:
-1 if x<y
0 if x=y
1 if x>y

Alex :p
eventprostrategiesAuthor Commented:
i answered my own question re: howto make "Default" collection methods for the "item" property of a collection here:

no one really answered howto implement a collection using both an integer indexer and string key for accessing items ... most efficiently.  I thank alex for some super basic sorting ... this question's dead tho.  i'll award some pts to alex -- cleanup, PAQ this puppy.
An interesting complete example of a custom collection implementation can be found here:

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.