. . Hong Kong Business Software Company
Expert in web-based solutions
Software House in Hong Kong

Microsoft Certified Solution Developer

 

Chief Architect's Blog on software development

2 April 2008
Comparing two objects using Operator Overload
Some years ago, I wrote a Quarter Allocation System for a government department using VB6. One of the requirements is to generate a priority list according to a very complicated criteria. Very briefly, they first compare all applicants' rank, then salary, then date promoted to that rank, then number of childern of the applicant, then date joined the government, then birthday, then ....

Life is much easier if I can use VB.Net for that project. .Net Framework 2.0 has an Operator Overload feature. You can create a class allowing two objects of the same class to compare with this format:

Emp1 > Emp2.

You can also implement your class with IComparable so that VB.Net know how to sort them according to your criteria. The following is a simplified Employee Class example (I further simply the comparison criteria for demonstration purpose).

   Public Class Employee
    Implements IComparable(Of Employee)

    Public Name As String
    Public Rank As RankType
    Public DateJoin As Date

    Public Sub New(ByVal aName As String, _
                    ByVal aRank As RankType, _
                    ByVal aDateJoin As Date)
        Name = aName
        Rank = aRank
        DateJoin = aDateJoin
    End Sub

#Region "Operator Overload"
    Public Shared Operator >(ByVal Emp1 As Employee,_
                             ByVal Emp2 As Employee) As Boolean
        If Emp1 Is Nothing OrElse Emp2 Is Nothing Then
            Throw New ArgumentNullException("Cannot compare with nothing")
        End If

        If Emp1.Rank = Emp2.Rank Then
            Return Emp1.DateJoin < Emp2.DateJoin
        Else
            Return Emp1.Rank > Emp2.Rank
        End If
    End Operator

    Public Shared Operator <(ByVal Emp1 As Employee, _
                    ByVal Emp2 As Employee) As Boolean
        Return Emp1 > Emp2
    End Operator

    Public Shared Operator =(ByVal Emp1 As Employee, _
                        ByVal Emp2 As Employee) As Boolean
        If Emp1.Rank = Emp2.Rank AndAlso _
            Emp1.DateJoin = Emp2.DateJoin Then
            Return True
        Else
            Return False
        End If
    End Operator

    Public Shared Operator <>(ByVal Emp1 As Employee, _
                ByVal Emp2 As Employee) As Boolean
        If Emp1 = Emp2 Then
            Return False
        Else
            Return True
        End If
    End Operator
#End Region

    Public Function CompareTo(ByVal other As Employee) As Integer _
             Implements System.IComparable(Of Employee).CompareTo
        'If Me = other Then
        '    Return 0
        'End If

        'If Equal Assuming Me is larger
        If other > Me Then
            Return -1
        Else
            Return 1
        End If
    End Function
End Class
Public Enum RankType
    Clerk = 1
    Officer = 2
    Executive = 3
    Manager = 4
    Director = 5
End Enum