Equals删除Equals中的错误分配 [英] Equals Remove wrong assignment inside Equals

查看:103
本文介绍了Equals删除Equals中的错误分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程,我正在用它来比较看起来像它的一些对象:

I have following class which i am using to compare some objects it looks like it:

 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

我能够正确比较比较例如:

I am able to compare correctly Compare e.g:

   Console.WriteLine("Was it found: " & parts.Contains(New Part() With { _
         .PartId = 1634, _
         .PartName = "shift lever" _
    }, New Part(EqualsComparmission.PartId_and_PartName)))

但是我在Remove上遇到问题,无论我发送给构造函数什么,该方法总是使用Equals第一个枚举值PartId,这是为什么?

but i got problem with Remove, the method is always going to Equals first enum value PartId, no matter what i send to constructor, why is that?

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

推荐答案

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

Assuming that parts is a List(Of Part), you can simplify your removal (and checking) by using 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匹配或两者都匹配的部分.这样,您就可以摆脱等于和枚举

You can just specify if you want to delete parts where the PartId matches or the PartName matches or both. This way you can get rid of the Equals and Enums

这篇关于Equals删除Equals中的错误分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆