Create Complex Structure with Equals and GetHashCode method
Public Structure Complex
Public re, im As Double
Public Overrides Function Equals(ByVal obj As [Object]) As Boolean
Return TypeOf obj Is Complex AndAlso Me = CType(obj, Complex)
End Function
Public Overrides Function GetHashCode() As Integer
Return re.GetHashCode() ^ im.GetHashCode()
End Function
Public Shared Operator = (x As Complex, y As Complex) As Boolean
Return x.re = y.re AndAlso x.im = y.im
End Operator
Public Shared Operator <> (x As Complex, y As Complex) As Boolean
Return Not (x = y)
End Operator
End Structure
Class Example
Public Shared Sub Main()
Dim cmplx1, cmplx2 As Complex
cmplx1.re = 4.0
cmplx1.im = 1.0
cmplx2.re = 2.0
cmplx2.im = 1.0
If cmplx1 <> cmplx2 Then
Console.WriteLine("The two objects are not equal.")
End If
If Not cmplx1.Equals(cmplx2) Then
Console.WriteLine("The two objects are not equal.")
End If
cmplx2.re = 4.0
If cmplx1.Equals(cmplx2) Then
Console.WriteLine("The two objects are now equal!")
End If
If cmplx1 = cmplx2 Then
Console.WriteLine("The two objects are now equal!")
End If
End Sub
End Class
Related examples in the same category