The .Net framework and especially the System.Collection namespace provides us two built in interfaces witch are IComparable and IComparer interfaces. The first one, namely, the IComparable specifies a behavior that allows an object to be compared to another one from the same type according to a specified member value such as a hash table value or so. At the other hand, IComparer interface enables us to compare two objects at once at the opposite of the first one witch enables us to compare two objects one by one. The IComparable and the IComparer could also be found as a part of the System.Collection. Generic built in interfaces, thing that renders the deal more comfortable when using them since the conversion exceptions headache will be neutralized and avoided with generic types.
In this article I will try to provide some techniques according to the effective use of the IComparable and the IComparer interfaces in a development context and I will expose some real use cases to know how, and under witch condition may one use each of both interfaces.
IComparable interface
Public Interface IComparable ' Methods Function CompareTo(ByVal obj As Object) As Integer End Interface |
The IComparable interface has Function CompareTo (ByVal o as Object) as Integer as a member. This last one is used to compare two objects one by one profiting of the extreme polymorphism nature of the interface. Say, that we have a type person and you want to compare two objects from this type according to their ages for example. It will be more practical to implement the IComparable interface within the person class core.
Public Class Person Implements IComparable ' Methods Public Sub New() End Sub
Public Sub New(ByVal FirstName As String, ByVal LastName As String, ByVal AgeAs Integer) Me.FirstName = FirstName Me.LastName = LastName Me.Age = Age End Sub
Public Function CompareTo(ByVal o As Object) As Integer Dim Temp As Person Temp = CType(o, Person) If (Me.Age < o.Age) Then Return 1 End If If (Me.Age > o.Age) Then Return -1 End If Return 0 End Function
' Properties Public Property Age As Integer Get Return Me._Age End Get Set(ByVal value As Integer) Me._Age = value End Set End Property
Public Property FirstName As String Get Return Me._FirstName End Get Set(ByVal value As String) Me._FirstName = value End Set End Property
Public Property LastName As String Get Return Me._LastName End Get Set(ByVal value As String) Me._LastName = value End Set End Property
' Fields Private _Age As Integer Private _FirstName As String Private _LastName As String End Class |
The method Function CompareTo (ByVal o as Object) as Integer returns an integer. You can specify your result according to the returned value as follow:
Class Program ' Methods Public Shared Sub Main(ByVal args As String()) Console.WriteLine("Hello World!") Dim I As New Person("Bejaoui", "Bechir", &H1D) Dim myFather As New Person("Habib", "Bejaoui", &H41) Dim myGrandFather As New Person("Krayem", "Bejaoui", &H5F) Select Case [I].CompareTo(myFather) Case 1 Console.WriteLine("My father is older than me") Exit Select Case -1 Console.WriteLine("I'm older than my father!!!") Exit Select Case 0 Console.WriteLine("My Father and I have the same age!") Exit Select End Select Console.Write("Press any key to continue . . . ") Console.ReadKey(True) End Sub
End Class
|
When the generics come into the world as a part of the .Net 2.0 innovations, the code structure becomes more robust as the problem that may raise a casting exception is avoided in this case. So the person class code structure will be as following:
Public Class Person Implements IComparable(Of Person) ' Methods Public Sub New() End Sub
Public Sub New(ByVal FirstName As String, ByVal LastName As String, ByVal AgeAs Integer) Me.FirstName = FirstName Me.LastName = LastName Me.Age = Age End Sub
Public Function CompareTo(ByVal o As Person) As Integer If (Me.Age < o.Age) Then Return 1 End If If (Me.Age > o.Age) Then Return -1 End If Return 0 End Function
' Properties Public Property Age As Integer Get Return Me._Age End Get Set(ByVal value As Integer) Me._Age = value End Set End Property
Public Property FirstName As String Get Return Me._FirstName End Get Set(ByVal value As String) Me._FirstName = value End Set End Property
Public Property LastName As String Get Return Me._LastName End Get Set(ByVal value As String) Me._LastName = value End Set End Property
' Fields Private _Age As Integer Private _FirstName As String Private _LastName As String End Class |
You can observe that the argument that overloads the CompareTo is a type of person rather than object now. With this manner the casting operation is avoided and the risk of receiving a compile or a run time errors are minimized. But let us be more practical as this example is demystifying the fact of profiting of the real IComparable interface services. I explain by giving a real example. The array type implements the IComparable interface in order to sort its objects collection and even the array list do the same thing to sort its objects collection internally.
Say that I want to build my own objects container type. For this, I build a new type called Room witch plays the role of person objects container.
The class person will be presented as follow:
Public Class Person ' Methods Public Sub New() End Sub
Public Sub New(ByVal FirstName As String, ByVal LastName As String, ByVal AgeAs Integer) Me.FirstName = FirstName Me.LastName = LastName Me.Age = Age End Sub
' Properties Public Property Age As Integer Get Return Me._Age End Get Set(ByVal value As Integer) Me._Age = value End Set End Property
Public Property FirstName As String Get Return Me._FirstName End Get Set(ByVal value As String) Me._FirstName = value End Set End Property
Public Property LastName As String Get Return Me._LastName End Get Set(ByVal value As String) Me._LastName = value End Set End Property
' Fields Private _Age As Integer Private _FirstName As String Private _LastName As String End Class
|

Join the conversation! Your thoughts help the community grow.