I am trying to add sorting support to a series of related classes. Here is a simplified and generic version which should show how things are laid out.
Class CItem
Class CSpecific1 inherits CItem
Class CSpecific2 inherits CItem
Class CSpecific3 inherits CSpecific1
... (several more like this)
Now CItem has some generic information that I would like to be able to sort on for any of the above classes (name and owner name are the most pressing). I have many lists and would like to do something like the following.
Dim lst as List(Of CSpecific2) = {get the list from somewhere}
lst.Sort(New CItemCompareOwner())
lst.Sort(New CItemCompareName())
...
I have tried adding the below compare class for the base class (CItem) which works on that specific class but does not work on other classes higher up the chain. I assume there is a way to have this work for all classes based off of CItem as I am only sorting on items in CItem. Doing this for each class would be ugly so I hope so. The specific error I get happens during runtime and is "Unable to case object of type 'Project.CItemCompareOwner' to type 'System.Collections.Generic.IComparer'1[Project.CSpecific1]'.
This is the compare class I am currently using. Also tried using "Implements iComparer" and using generic objects but that didn't work either. I can't remember if it was a similar exception though.
Class CItemCompareOwner
implements iComparer(Of CItem)
Public Function Compare(ByVal first as CItem, ByVal second as CItem) _
implemets System.Collections.Generic.IComparer(Of CItem).Compare
return first.Owner.Name.CompareTo(second.Owner.Name)
end function
end class
This is pretty new to me so any help would be appreciated.
Class CItem
Class CSpecific1 inherits CItem
Class CSpecific2 inherits CItem
Class CSpecific3 inherits CSpecific1
... (several more like this)
Now CItem has some generic information that I would like to be able to sort on for any of the above classes (name and owner name are the most pressing). I have many lists and would like to do something like the following.
Dim lst as List(Of CSpecific2) = {get the list from somewhere}
lst.Sort(New CItemCompareOwner())
lst.Sort(New CItemCompareName())
...
I have tried adding the below compare class for the base class (CItem) which works on that specific class but does not work on other classes higher up the chain. I assume there is a way to have this work for all classes based off of CItem as I am only sorting on items in CItem. Doing this for each class would be ugly so I hope so. The specific error I get happens during runtime and is "Unable to case object of type 'Project.CItemCompareOwner' to type 'System.Collections.Generic.IComparer'1[Project.CSpecific1]'.
This is the compare class I am currently using. Also tried using "Implements iComparer" and using generic objects but that didn't work either. I can't remember if it was a similar exception though.
Class CItemCompareOwner
implements iComparer(Of CItem)
Public Function Compare(ByVal first as CItem, ByVal second as CItem) _
implemets System.Collections.Generic.IComparer(Of CItem).Compare
return first.Owner.Name.CompareTo(second.Owner.Name)
end function
end class
This is pretty new to me so any help would be appreciated.