IEqualityComparer为'='overloader

时间:2012-12-28 19:49:11

标签: vb.net

我的问题是我的一般理解,这不是问题,以下是我项目的演示类:

    Public Class Registration
        Inherits HumanBase

        Private _NameeValue As HumanName
        Property Namee As HumanName
            Get
                Return _NameeValue
            End Get
            Set(ByVal Value As HumanName)
                If Not _NameeValue = Value Then
                    _NameeValue = Value
                    OnPropertyChanged("Namee")
                End If
            End Set
        End Property
        Private _UserPasswordValue As String
        Public Property UserPassword() As String
            Get
                Return _UserPasswordValue
            End Get
            Set(ByVal value As String)
                If Not _UserPasswordValue.Equals(value) Then
                    _UserPasswordValue = value
                    OnPropertyChanged("UserPassword")
                End If
            End Set
        End Property

        Private _UserEmailValue As String
        Public Property UserEmail() As String
            Get
                Return _UserEmailValue
            End Get
            Set(ByVal value As String)
                If Not _UserEmailValue = value Then
                    _UserEmailValue = value
                    OnPropertyChanged("UserEmail")
                End If

            End Set
        End Property

        Property UserPassword2 As String
    End Class
Public Class HumanName
        Inherits HumanBase
        Implements IComparable(Of HumanName)
        Implements IEqualityComparer(Of HumanName)



#Region "Private Variables"
        Private _TitleNameValue As CommonTypes.TitleName
        Private _FirstNameValue As String
        Private _MiddleNameValue As String
        Private _LastNameValue As String
        Private _SuffixNameValue As CommonTypes.Suffixname
#End Region

#Region "Property Variables"
        Property TitleName As CommonTypes.TitleName
            Get
                Return _TitleNameValue
            End Get
            Set(ByVal Value As CommonTypes.TitleName)
                If Not _TitleNameValue = Value Then
                    _TitleNameValue = Value
                    OnPropertyChanged("TitleName")
                End If
            End Set
        End Property
        Property FirstName As String
            Get
                Return _FirstNameValue
            End Get
            Set(ByVal Value As String)
                If Not _FirstNameValue = Value Then
                    _FirstNameValue = Value
                    OnPropertyChanged("FirstName")
                End If
            End Set
        End Property
        Property MiddleName As String
            Get
                Return _MiddleNameValue
            End Get
            Set(ByVal Value As String)
                If Not _MiddleNameValue = Value Then
                    _MiddleNameValue = Value
                    OnPropertyChanged("MiddleName")
                End If
            End Set
        End Property
        Property LastName As String
            Get
                Return _LastNameValue
            End Get
            Set(ByVal Value As String)
                If Not _LastNameValue = Value Then
                    _LastNameValue = Value
                    OnPropertyChanged("LastName")
                End If
            End Set
        End Property
        Property SuffixName As CommonTypes.Suffixname
            Get
                Return _SuffixNameValue
            End Get
            Set(ByVal Value As CommonTypes.Suffixname)
                If Not _SuffixNameValue = Value Then
                    _SuffixNameValue = Value
                    OnPropertyChanged("SuffixName")
                End If
            End Set
        End Property
#End Region

        Public Function CompareTo(ByVal other As HumanName) As Integer Implements System.IComparable(Of HumanName).CompareTo
            Return True
        End Function

        Public Function Equals1(ByVal x As HumanName, ByVal y As HumanName) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of HumanName).Equals
            Return True
        End Function

        Public Function GetHashCode1(ByVal obj As HumanName) As Integer Implements System.Collections.Generic.IEqualityComparer(Of HumanName).GetHashCode
            Return True
        End Function
    End Class

代码在编码时出错“If Not _NameeValue = Value Then”,错误为Error Operator '=' is not defined for types 'SharesCommCodeLib.Human.HumanName' and 'SharesCommCodeLib.Human.HumanName'

当我实现ICOMPAREABLE和IEquitableComparer时,你能否告诉我为什么'='不起作用.DotNet会不会将它视为运算符重载。

谢谢。

1 个答案:

答案 0 :(得分:0)

  1. 我强烈建议设置OPTION STRICT to ON,当您尝试从签名定义Boolean作为返回类型的方法返回Integer时,您的代码甚至不会编译。这有助于避免错误
  2. IEqualityComparer接口用于定义您自己的相等定义,并且可以在此接口用于指定相等性的任何位置使用,例如在HashtableNameValueCollectionOrderedDictionary的构造函数中=
  3. 如果您想在自定义类型上使用Public Shared Operator =(ByVal humanName1 As HumanName, ByVal humanName2 As HumanName) As Boolean Return humanName1._FirstNameValue.Equals(humanName2._FirstNameValue) AndAlso _ humanName1._MiddleNameValue.Equals(humanName2._MiddleNameValue) AndAlso _ humanName1._LastNameValue.Equals(humanName2._LastNameValue) End Operator Public Shared Operator <>(ByVal humanName1 As HumanName, ByVal humanName2 As HumanName) As Boolean Return Not humanName1 = humanName2 End Operator 运算符,则需要overload it

  4. {{1}}