Enum在Equals()中设置为默认值

时间:2015-01-08 19:37:39

标签: vb.net

我收到了这段代码:

Imports System.Collections.Generic

Public Class Part
    Implements IEqualityComparer(Of Part)

    Public Property _comparisonType As EqualsComparmission

    Public Sub New(ComparisonType As EqualsComparmission)
        Me._comparisonType = ComparisonType
    End Sub

    Public Sub New()
    End Sub

    Public Property PartName() As String
    Public Property PartId() As Integer

    Public Overrides Function ToString() As String
        Return "ID: " & PartId & "   Name: " & PartName
    End Function


    Public Function Equals12222(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 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

如果存在,我就会从列表中删除内容:

parts.Remove(New Part(EqualsComparmission.PartId_and_PartName) With { _
         .PartId = 11, _
         .PartName = "ala" _
    })

问题是,当代码命中Equals方法时,它总是选择第一个枚举默认值,所以:

Select Case _comparisonType
         Case EqualsComparmission.PartId
              result = Me.PartId.Equals(objAsPart.PartId)

我还通过手表逐行调试代码。

首先是:

Public Sub New(ComparisonType As EqualsComparmission)
        Me._comparisonType = ComparisonType
    End Sub

并正确设置

的值
Public Property _comparisonType As EqualsComparmission

EqualsComparmission.PartId_and_PartName

然后调试器又回到了行:

parts.Remove(New Part(EqualsComparmission.PartId_and_PartName) With { _
       .PartId = 11, _
       .PartName = "ala" _
  })

然后直接进入Equals,现在该属性等于PartId !! ??

1 个答案:

答案 0 :(得分:1)

列表中使用简单ctor的所有项目都设置为PartId比较。你的测试代码:

parts.Remove(New Part(EqualsComparmission.PartId_and_PartName) With { _
       .PartId = 11, _
       .PartName = "ala" _
  })

将迭代该列表以查找导致以下内容的匹配项:

If parts(n).Equals(newPart) Then

在方法中创建的Part / Item是一个参数,因此它是RHS。所有列表项都设置为PartID,因此用于比较。解决问题的一种方法是你如何称呼它:

Dim plist As New List(Of Part)
plist.Add(New Part With {.PartId = 42, .PartName = "screw"})
plist.Add(New Part With {.PartId = 14, .PartName = "nail"})

Dim testP As New Part(EqualsComparmission.PartId_and_PartName) With {
 .PartId = 42,
 .PartName = "screw"
     }

For n As Integer = plist.Count - 1 To 0 Step -1
    If testP.Equals(plist(n)) Then        ' using Equals from the Temp Part
        plist.RemoveAt(n)
        ' maybe Exit For if you expect only one
    End If
Next

你也可以重载Equals:

Public Overloads Function Equals(obj As Object, 
          compareType As EqualsComparmission) As Boolean
...
If testP.Equals(plist(0), EqualsComparmission.PartId_and_PartName) Then

这将允许您在调用时指定compare方法,但这不允许您像使用它一样使用它。您的列表正在列表项上调用Equals方法,它将使用Equals(obj As Object)版本。