samsmithnz Posted February 24, 2004 Posted February 24, 2004 I have a class (clsItem), that I use to inherit in all my other classes. It looks like this (very straight forward): Public Class clsItem Implements IComparer Private intItemKey As Integer Private strName As String Private lngBuyPrice As Long Overridable Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare Compare = x.ItemKey < y.ItemKey End Function Public Property ItemKey() As Integer Get ItemKey = intItemKey End Get Set(ByVal Value As Integer) intItemKey = Value End Set End Property Public Property Name() As String Get Name = strName End Get Set(ByVal Value As String) strName = Value End Set End Property Public Property BuyPrice() As Long Get BuyPrice = lngBuyPrice End Get Set(ByVal Value As Long) lngBuyPrice = Value End Set End Property End Class I have about 10 other classes that inherit this clsItem. I then read from a database and load each type of item into the right class, then each object is loaded into an arraylist. I'm now trying to sort the collection on the ItemKey field (inherited from clsItem). Unfortunetly this isn't working out. I get this error: System.InvalidOperationException: Specified IComparer threw an exception. ---> System.ArgumentException: At least one object must implement IComparable. at System.Collections.Comparer.Compare(Object a, Object b) at System.SorterObjectArray.QuickSort(Int32 left, Int32 right) --- End of inner exception stack trace --- at System.SorterObjectArray.QuickSort(Int32 left, Int32 right) at System.Array.Sort(Array keys, Array items, Int32 index, Int32 length, IComparer comparer) at System.Array.Sort(Array array, Int32 index, Int32 length, IComparer comparer) at System.Collections.ArrayList.Sort(Int32 index, Int32 count, IComparer comparer) at System.Collections.ArrayList.Sort() at XClone.modDataAccess.LoadUFOPedia(XmlDocument objXMLDoc) in C:\Projects\Xclone\modDataAccess.vb:line 121 What do I have to do to get this to work? I thought that implementing my overidden compare function in the clsItem would be enough, but this error message seems to be saying otherwise? any ideas brainy-acs??? Quote Thanks Sam http://www.samsmith.co.nz
samsmithnz Posted February 24, 2004 Author Posted February 24, 2004 If this isn't possible, maybe I should write my own sorting algorithm??? Quote Thanks Sam http://www.samsmith.co.nz
Administrators PlausiblyDamp Posted February 24, 2004 Administrators Posted February 24, 2004 insteam of implementing IComparer try using IComparable, delete the implements statement and your Compare function and paste this in it's place and see if it works. Implements IComparable Public Function CompareTo1(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo Dim x As clsItem = DirectCast(obj, clsItem) If ItemKey > x.ItemKey Then Return 1 If ItemKey < x.ItemKey Then Return -1 Return 0 End Function Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
samsmithnz Posted February 25, 2004 Author Posted February 25, 2004 Thanks PlausiblyDamp, you're amazing, that works perfectly! Quote Thanks Sam http://www.samsmith.co.nz
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.