问题
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)))
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" _
})
回答1:
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")
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
来源:https://stackoverflow.com/questions/27842628/equals-remove-wrong-assignment-inside-equals