Quote Originally Posted by jmcilhinney
That MSDN link is for the IComparable.CompareTo method. I've been telling you to implement IComparable from the start. As I also said, you should be implementing the generic version of the interface in .NET 2.0. If you implement the standard IComparable interface then your code would look something like this:
vb Code:
  1. Public Class Thing
  2.     Implements IComparable
  3.  
  4.     Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
  5.         If Not TypeOf obj Is Thing Then
  6.             Throw New ArgumentException("Object is not of type Thing.", "obj")
  7.         End If
  8.  
  9.         Dim otherThing As Thing = DirectCast(obj, Thing)
  10.  
  11.         'Compare Me with otherThing here.
  12.     End Function
  13.  
  14. End Class
Note that the object being compared to is passed via an Object reference, which means that you must check its type at run time and allow for it to be the wrong type. If you implement the generic version of the interface that is taken care of at compile time.
vb Code:
  1. Public Class Thing
  2.     Implements IComparable(Of Thing)
  3.  
  4.     Public Function CompareTo(ByVal other As Thing) As Integer Implements System.IComparable(Of Thing).CompareTo
  5.         'Compare Me with other here.
  6.     End Function
  7.  
  8. End Class
It's not possible to pass any object that is not type Thing so you don't have to test its type or cast and there's no possibility of an exception.
thanks for the code... i follow the example at the link and implemented the standard interface... i will change to a generic one, like you posted... thx