0

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

    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'

你能告诉我为什么当我同时实现 ICOPAREABLE 和 IEquitableComparer 时 '=" 不起作用。DotNet 不会考虑将它用于运算符重载吗?

谢谢你。

4

1 回答 1

0
  1. 我强烈建议设置OPTION STRICT为 ONBoolean ,当您尝试从签名定义Integer为返回类型的方法返回 a 时,您的代码甚至不会编译。这有助于避免错误
  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
于 2012-12-28T19:56:00.437 回答