Operator equal to (=) Overloading example
Well I’ve not encountered many scenarios where I really needed to overload an operator. However operator overloading can save you a lot of coding time like in the double data entered application example I worked on and applied the concept.
This is how it goes.
Two users (first and second entry user) will enter two entries of the same record, it doesn’t matter who enters the first though. If both users enter correctly, the two entries will be merged into a final verified entry. Otherwise both entries will be saved and compared for error(s) before merging them.
This case needed retrieving a previously saved entry by the first user, compare it with one about to be saved, and if they’re the same, update the saved copy to verified otherwise display fields that are different for both users for editing.
Operator equal to (=) Overloading was used as a short cut to this implementation, a simplified code sample is as follows
Public Class Customer
#Region " Fields "
Private m_CustID As Integer
Private m_FirstName As String
Private m_LastName As String
Private m_Address As String
Private m_Telephone As String
Private m_Email As String
#End Region
#Region " Properties "
Public Property CustID() As Integer
Get
Return m_CustID
End Get
Set(ByVal Value As Integer)
m_CustID = Value
End Set
End Property
Public Property FirstName() As String
Get
Return m_FirstName
End Get
Set(ByVal Value As String)
m_FirstName = Value
End Set
End Property
Public Property LastName() As String
Get
Return m_LastName
End Get
Set(ByVal Value As String)
m_LastName = Value
End Set
End Property
Public Property Address() As String
Get
Return m_Address
End Get
Set(ByVal Value As String)
m_Address = Value
End Set
End Property
Public Property Telephone() As String
Get
Return m_Telephone
End Get
Set(ByVal Value As String)
m_Telephone = Value
End Set
End Property
Public Property Email() As String
Get
Return m_Email
End Get
Set(ByVal Value As String)
m_Email = Value
End Set
End Property
#End Region
#Region " Constructors "
Public Sub New()
MyBase.New()
End Sub
#End Region
#Region " Operator = Overloading "
''' <summary>
''' Overloading = operator is as simple as creating a method.
''' In fact, operator overloads are really just methods created with the Operator keyword
''' Note that the comaprison excludes CustID field, which is the primary key
''' </summary>
''' <param name="lhs"></param>
''' <param name="rhs"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Operator =(ByVal lhs As Customer, ByVal rhs As Customer) As Boolean
If lhs Is Nothing OrElse rhs Is Nothing Then Return False
If lhs.FirstName.ToUpper() <> rhs.FirstName.ToUpper() Then Return False
If lhs.LastName.ToUpper() <> rhs.LastName.ToUpper() Then Return False
If lhs.Address.ToUpper() <> rhs.Address.ToUpper() Then Return False
If lhs.Telephone <> rhs.Telephone Then Return False
If lhs.Email <> rhs.Email Then Return False
Return True
End Operator
''' <summary>
''' When you define = operator, VB requires that you also
''' define the operator for the inverse operation (not equal to) operator.
''' Simply reverse the = operand and use the not equal to operator
''' </summary>
''' <param name="lhs"></param>
''' <param name="rhs"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Operator <>(ByVal lhs As Customer, ByVal rhs As Customer) As Boolean
Return Not lhs = rhs
End Operator
#End Region
#Region " Methods "
#End Region
End Class
You can now use equal to (=) as shown in the following code
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
'Get the previously saved customer 101, by user one.
'For simplicity, we will just assign this customer as follows
Dim oCustByUser1 As New Customer() _
With {.CustID = 101, .FirstName = "Wilson", .LastName = "Kutegeka", _
.Address = "Kampala", .Telephone = "+256 772 609113"}
'Pick the new customer record 101, by user two from your interface.
'For simplicity, we will assign this customer as well
Dim oCustByUser2 As New Customer() _
With {.CustID = 101, .FirstName = "Wilson", .LastName = "Kutegeka", _
.Address = "Kampala-Uganda", .Telephone = "+256 772 609113"}
'This will not verify because adress is not the same
If oCustByUser1 = oCustByUser2 Then
'Update the first entry to verified
MessageBox.Show("Verified!")
Else
'Check to see which fields are not the same and report to user
MessageBox.Show("Some Fields are not the same!")
End If
End Sub