Equals 删除 Equals 中的错误赋值

Equals Remove wrong assignment inside Equals

我有以下 class,我用它来比较它看起来像的一些对象:

 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 时遇到了问题,该方法总是等于第一个枚举值 PartId,无论我向构造函数发送什么,这是为什么?

 Dim deleted As Boolean = parts.Remove(New Part(EqualsComparmission.PartId_and_PartName) With { 
             .PartId = 11, _
             .PartName = "al7a" _
        })

假设 partsList(Of Part),您可以使用 linq 来简化删除(和检查):

'check it exists
Debug.WriteLine("Was it found: " & parts.Exists(Function(x) x.PartId = 1634 And x.PartName = "shift lever"))

'Remove by PartId and PartName
Dim deleted As Integer = parts.RemoveAll(Function(x) x.PartId = 1634 And x.PartName = "shift lever")
'or just by PartId
Dim deleted As Integer = parts.RemoveAll(Function(x) x.PartId = 1634)
'or just by PartName
Dim deleted As Integer = parts.RemoveAll(Function(x) x.PartName = "shift lever")

您可以只指定是否要删除 PartId 匹配或 PartName 匹配或两者都匹配的部分。这样你就可以去掉 Equals 和 Enums