Re: Sorting a System.Collections.ObjectModel.Collection

Tech-Archive recommends: Speed Up your PC by fixing your registry



Arthur,

Thinking about it, it might be better to ensure that you have a wrapped
List(Of T) verses some other IList(Of T) collection... Especially when
introducing a new generic base class...

| > Public Class CollectionBase(Of T)
| > Inherits ObjectModel.Collection(Of T)
| >
Public Sub New()
MyBase.New()
End Sub

Public Sub New(list As IList(Of T))
MyBase.New(list)
End Sub

| > Public Sub Sort()
Dim list As List(Of T) = TryCast(Items, List(Of T))
If list Is Nothing Then Throw New NotSupportedException()
list.Sort()
| > End Sub
| >

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"Arthur Dent" <hitchhikersguideto-news@xxxxxxxxx> wrote in message
news:%23YskK%238TGHA.4540@xxxxxxxxxxxxxxxxxxxxxxx
| Thank you, that was exactly what i was looking for, getting at the inner
| list object to be able to use its methods.
| Cheers!
|
|
| "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@xxxxxxxxxxxxx> wrote in
| message news:eUytha0TGHA.2276@xxxxxxxxxxxxxxxxxxxxxxx
| > Arthur,
| > In addition to the other comments:
| >
| >
| > Remember that Collection(Of T) is simply a wrapper for an object that
| > implements IList(Of T). This wrapped IList(Of T) does all the "heavy
| > lifting" the Collection(Of T) simply delegates to the wrapped IList(Of
T).
| > <sidenote>Read Collection(Of T) uses the Strategy Pattern. The IList(Of
| > T)
| > is the strategy it uses to implement the list itself, by default its
uses
| > an
| > array like structure for the strategy (the List(Of T), you can easily
| > replace the strategy with a LinkedList or a Binary tree, see below for
| > details on replacing the strategy...</sidenote>
| >
| > You can use Collection(Of T).Items to get at the wrapped IList(Of T).
| >
| > By default Collection(Of T) uses a List(Of T) as the wrapped IList(Of
T),
| > List(Of T) has a Sort method (plus a plethora of other methods).
| > Collection(Of T).Items is how you get at the wrapped IList(Of T). In
other
| > words casting Collection(Of T).Items to List(Of T) should allow you to
| > call
| > its Sort method.
| >
| > Something like:
| >
| > Public Class Person
| > Implements IComparable(Of Person)
| >
| > ...
| >
| > Public Function CompareTo(ByVal other As Person) As Integer
| > Implements System.IComparable(Of Person).CompareTo
| > ...
| > End Function
| >
| > End Class
| >
| > Public Class PersonCollection
| > Inherits ObjectModel.Collection(Of Person)
| >
| > Public Sub Sort()
| > DirectCast(Items, List(Of Person)).Sort()
| > End Sub
| >
| > End Class
| >
| > I would consider introducing a new generic collection base that
| > incorporates
| > this sorting:
| >
| > Public Class CollectionBase(Of T)
| > Inherits ObjectModel.Collection(Of T)
| >
| > Public Sub Sort()
| > DirectCast(Items, List(Of T)).Sort()
| > End Sub
| >
| > Public Sub Sort(ByVal comparer As IComparer(Of T))
| > DirectCast(Items, List(Of T)).Sort(comparer)
| > End Sub
| >
| > Public Sub Sort(ByVal index As Integer, ByVal count As Integer,
| > ByVal comparer As IComparer(Of T))
| > DirectCast(Items, List(Of T)).Sort(index, count, comparer)
| > End Sub
| >
| > Public Sub Sort(ByVal comparison As Comparison(Of T))
| > DirectCast(Items, List(Of T)).Sort(comparison)
| > End Sub
| >
| > End Class
| >
| > Then PersonCollection would be based on this new base:
| >
| > Public Class PersonCollection
| > Inherits CollectionBase(Of Person)
| >
| > End Class
| >
| >
| > NOTE: The above may fail if you call Collection(Of T).New(IList(Of T))
| > passing in a something other then List(Of T). For example:
| >
| > Public Class PersonCollection
| > Inherits ObjectModel.Collection(Of Person)
| >
| > ' use a linked list strategy
| > Public Sub New()
| > MyBase.New(New LinkedList(Of Person))
| > End Sub
| >
| > ' use an array strategy
| > Public Sub New()
| > MyBase.New(New Person(100-1) {}) ' base it on an 100 element
| > array
| > End Sub
| >
| > ' use some other generic collection strategy
| > ' where SpecializedCollection implements IList(Of T)
| > Public Sub New()
| > MyBase.New(New SpecializedCollection(Of Person))
| > End Sub
| >
| > End Class
| >
| > The Frameworks LinkedList(Of T) does not implement IList(Of T) although
it
| > is easy to add. The array does work (as the wrapped list), giving you a
| > fixed sized collection that you cannot add or remove elements from (you
| > can
| > only change existing entries)...
| >
| > I have not yet posted my sample LinkedList(Of T) that implements
IList(Of
| > T)
| > to my web site...
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Arthur Dent" <hitchhikersguideto-news@xxxxxxxxx> wrote in message
| > news:eAOTJuVTGHA.1728@xxxxxxxxxxxxxxxxxxxxxxx
| > | How do you sort a generic collection derived from
| > | System.Collections.ObjectModel.Collection?
| > |
| > | Thanks in advance,
| > | - Arthur Dent
| > |
| > |
| >
| >
|
|


.



Relevant Pages

  • Re: Sorting a System.Collections.ObjectModel.Collection
    ... Remember that Collectionis simply a wrapper for an object that ... Listhas a Sort method. ... Public Class PersonCollection ... Public Sub Sort() ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Explanation of wrapper class
    ... Joanna Carter [TeamB] wrote: ... The Wrapper pattern is also known as the Adapter patern and this is ... public class VirtualAdapter ...
    (microsoft.public.dotnet.general)
  • Re: ListView problem
    ... I use approach for calling .NET form from vb6 and VBA apps but I use ... when I expose wrapper to COM I am able to open form as from any other ... public class MyForm: Form ... DialogResult res = frm.ShowDialog; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: inconsistent accessibility error
    ... Wrapper, and E is defined in Wrapper ... Marc ... >> public class Wrapper { ... >> Inconsistent accessibility: parameter type Wrapper.E' is less accessible ...
    (microsoft.public.dotnet.languages.csharp)