Skip to content

Instantly share code, notes, and snippets.

@vbjay
Created July 19, 2017 16:17
Show Gist options
  • Save vbjay/f4b8640cc78802222136a04a0863a85c to your computer and use it in GitHub Desktop.
Save vbjay/f4b8640cc78802222136a04a0863a85c to your computer and use it in GitHub Desktop.
Interface Equality check
Imports EqCheck
Module Module1
Sub Main()
Dim items As Tester() = {
New A,
New A(New Guid("d5dba793-32b5-4f82-b0a2-5c6439e830c4"), New Guid("a68cd6a0-d635-45ad-a191-78b3acd65c68")),
New A(New Guid("d5dba793-32b5-4f82-b0a2-5c6439e830c4"), New Guid("a68cd6a0-d635-45ad-a191-78b3acd65c68")),
New B(),
New B(New Guid("d5dba793-32b5-4f82-b0a2-5c6439e830c4"), New Guid("a68cd6a0-d635-45ad-a191-78b3acd65c68")),
New B(New Guid("d5dba793-32b5-4f82-b0a2-5c6439e830c4"), New Guid("a68cd6a0-d635-45ad-a191-78b3acd65c68"))
}
For Each test In items
Dim t1 = test
Console.WriteLine($"Checking {test}")
For Each t2 In items
Dim other = t2
Console.WriteLine($"{vbTab}Other:{other} is {(If(t1.Equals(other), "Equal", "NOT equal"))}")
Next
Next
Console.ReadKey()
End Sub
End Module
Interface Tester
Property ID As Guid
Property ID2 As Guid
Property Name As String
End Interface
Class A
Implements Tester
Public Sub New()
ID = Guid.NewGuid
ID2 = Guid.NewGuid
End Sub
Public Sub New(ByVal iD As Guid, ByVal iD2 As Guid)
Me.ID = iD
Me.ID2 = iD2
End Sub
Public Property ID As Guid Implements Tester.ID
Public Property ID2 As Guid Implements Tester.ID2
Public Property Name As String Implements Tester.Name
Overloads Function Equals(other As Tester) As Boolean
If ReferenceEquals(Nothing, other) Then
Return False
End If
If ReferenceEquals(Me, other) Then
Return True
End If
Return ID.Equals(other.ID) AndAlso ID2.Equals(other.ID2)
End Function
Overloads Function Equals(other As A) As Boolean
If ReferenceEquals(Nothing, other) Then
Return False
End If
If ReferenceEquals(Me, other) Then
Return True
End If
Return ID.Equals(other.ID) AndAlso ID2.Equals(other.ID2)
End Function
Overrides Function Equals(obj As Object) As Boolean
If TypeOf obj Is A Then
Return Equals(CType(obj, A))
ElseIf TypeOf obj Is Tester Then
Return Equals(CType(obj, Tester))
End If
Return MyBase.Equals(obj)
End Function
Overrides Function GetHashCode() As Integer
Dim hashCode = 47L
hashCode = CInt((hashCode * 53 + ID.GetHashCode()) And &H7FFFFFFFL)
hashCode = CInt((hashCode * 53 + ID2.GetHashCode()) And &H7FFFFFFFL)
Return CInt(hashCode)
End Function
Public Overrides Function ToString As String
Return $"ID: {ID }, ID2: {ID2 }, Name: {Me.GetType.FullName } "
End Function
End Class
Class B
Implements Tester
Public Sub New()
ID = Guid.NewGuid
ID2 = Guid.NewGuid
End Sub
Public Sub New(ByVal iD As Guid, ByVal iD2 As Guid)
Me.ID = iD
Me.ID2 = iD2
End Sub
Public Property ID As Guid Implements Tester.ID
Public Property ID2 As Guid Implements Tester.ID2
Public Property Name As String Implements Tester.Name
Overloads Function Equals(other As Tester) As Boolean
If ReferenceEquals(Nothing, other) Then
Return False
End If
If ReferenceEquals(Me, other) Then
Return True
End If
Return ID.Equals(other.ID) AndAlso ID2.Equals(other.ID2)
End Function
Overloads Function Equals(other As B) As Boolean
If ReferenceEquals(Nothing, other) Then
Return False
End If
If ReferenceEquals(Me, other) Then
Return True
End If
Return ID.Equals(other.ID) AndAlso ID2.Equals(other.ID2)
End Function
Overrides Function Equals(obj As Object) As Boolean
If TypeOf obj Is B Then
Return Equals(CType(obj, B))
ElseIf TypeOf obj Is Tester Then
Return Equals(CType(obj, Tester))
End If
Return MyBase.Equals(obj)
End Function
Overrides Function GetHashCode() As Integer
Dim hashCode = 47L
hashCode = CInt((hashCode * 53 + ID.GetHashCode()) And &H7FFFFFFFL)
hashCode = CInt((hashCode * 53 + ID2.GetHashCode()) And &H7FFFFFFFL)
Return CInt(hashCode)
End Function
Public Overrides Function ToString As String
Return $"ID: {ID }, ID2: {ID2 }, Name: {Me.GetType.FullName }"
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment