次のクラスを使用して、次のように見えるいくつかのオブジェクトを比較しています。
Imports System.Collections.Generic
Public Class Part
Implements IEqualityComparer(Of Part)
Public _comparisonType As EqualsComparmission
Public Sub New(ComparisonType As EqualsComparmission)
Me._comparisonType = ComparisonType
End Sub
Public Sub New()
End Sub
Public Property PartName() As String
Get
Return m_PartName
End Get
Set(value As String)
m_PartName = value
End Set
End Property
Private m_PartName As String
Public Property PartId() As Integer
Get
Return m_PartId
End Get
Set(value As Integer)
m_PartId = value
End Set
End Property
Private m_PartId As Integer
Public Overrides Function ToString() As String
Return "ID: " & PartId & " Name: " & PartName
End Function
Public Function Equals1(x As Part, y As Part) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of Part).Equals
If x Is Nothing AndAlso y Is Nothing Then Return True
If x Is Nothing OrElse y Is Nothing Then Return False
Select Case _comparisonType
Case EqualsComparmission.PartId
Return x.PartId = y.PartId
Case EqualsComparmission.PartName
Return String.Equals(x.PartName, y.PartName)
Case EqualsComparmission.PartId_and_PartName
Return x.PartId = y.PartId AndAlso String.Equals(x.PartName, y.PartName)
Case Else
Throw New NotSupportedException("Unknown comparison type for parts: " & _comparisonType.ToString())
End Select
End Function
Public Function GetHashCode1(obj As Part) As Integer Implements System.Collections.Generic.IEqualityComparer(Of Part).GetHashCode
Select Case _comparisonType
Case EqualsComparmission.PartId
Return obj.PartId
Case EqualsComparmission.PartName
Return If(obj.PartName Is Nothing, 0, obj.PartName.GetHashCode())
Case EqualsComparmission.PartId_and_PartName
Dim hash = 17
hash = hash * 23 + obj.PartId
hash = hash * 23 + If(obj.PartName Is Nothing, 0, obj.PartName.GetHashCode())
Return hash
Case Else
Throw New NotSupportedException("Unknown comparison type for parts: " & _comparisonType.ToString())
End Select
End Function
Public Overrides Function Equals(obj As Object) As Boolean
If obj Is Nothing Then
Return False
End If
Dim objAsPart As Part = TryCast(obj, Part)
Dim result As Boolean = False
Select Case _comparisonType
Case EqualsComparmission.PartId
result = Me.PartId.Equals(objAsPart.PartId)
Case EqualsComparmission.PartName
result = Me.PartName.Equals(objAsPart.PartName)
Case EqualsComparmission.PartId_and_PartName
result = Me.PartId.Equals(objAsPart.PartId) And Me.PartName.Equals(objAsPart.PartName)
Case Else
Throw New NotSupportedException("Unknown comparison type for parts: " & _comparisonType.ToString())
End Select
Return result
End Function
End Class
Public Enum EqualsComparmission
PartId
PartName
PartId_and_PartName
End Enum
私は正しく比較することができます比較例:
Console.WriteLine("Was it found: " & parts.Contains(New Part() With { _
.PartId = 1634, _
.PartName = "shift lever" _
}, New Part(EqualsComparmission.PartId_and_PartName)))
しかし、Remove で問題が発生しました。コンストラクターに何を送信しても、メソッドは常に Equals の最初の列挙値 PartId になります。なぜですか?
Dim deleted As Boolean = parts.Remove(New Part(EqualsComparmission.PartId_and_PartName) With {
.PartId = 11, _
.PartName = "al7a" _
})