Equals在Equals中删除错误的赋值

时间:2015-01-08 14:38:02

标签: .net vb.net iequalitycomparer iequatable

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

 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)))

但我遇到了删除问题,该方法总是等于第一个枚举值PartId,无论我发送给构造函数,为什么会这样?

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

1 个答案:

答案 0 :(得分:1)

假设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