Quantcast
Channel: YouTrackReSharper (RSRP) - Bug and Issue Tracker
Viewing all articles
Browse latest Browse all 106942

RSRP-287997: VB.NET: 'Generate Equality Operators' doesn't produce compilable code and GetHashCode method is wrong

$
0
0
Reporter Denis Abramov (sparky2708) Denis Abramov (sparky2708)
Created Jan 25, 2012 7:45:42 PM
Updated Jan 25, 2012 7:50:24 PM
Priority Normal
Type Unspecified
Fix versions No Fix versions
State Submitted
Assignee Unassigned
Subsystem No subsystem
Affected versions No Affected versions
Fixed in build No Fixed in build
Suppose I have:

 Public Class ApprovalInfo
        Public Property Field As String
        Public Property Exists As Boolean
        Public Property Signer As String
        Public Property TimeStamp As Date
        Private Shared ReadOnly theLogger As IEMGLog = EMGLogManager.GetLogger(GetType(ApprovalInfo))

        Public Sub New(ByVal approvalField As String, ByVal approvalExists As Boolean, ByVal approvalSigner As String, ByVal approvalTimeStamp As Date)
            theLogger.Trace("Calling ApprovalInfo.New")
            Me.Field = approvalField
            Me.Exists = approvalExists
            Me.Signer = approvalSigner
            Me.TimeStamp = approvalTimeStamp
        End Sub

        Public Overrides Function ToString() As String
            theLogger.Trace("Calling ApprovalInfo.ToString")
            Return String.Format("[{0}] ApprovalExists: {1} Signer: '{2}' TimeStamp: '{3}'", Field, Exists, Signer, TimeStamp)
        End Function
    End Class



I click on "Generate Equality Members" (see screenshot). So there are 2 problems here:

(1) I get the following WHICH DOESN'T COMPILE:

 Public Class ApprovalInfo
        Public Property Field As String
        Public Property Exists As Boolean
        Public Property Signer As String
        Public Property TimeStamp As Date
        Private Shared ReadOnly theLogger As IEMGLog = EMGLogManager.GetLogger(GetType(ApprovalInfo))

        Public Sub New(ByVal approvalField As String, ByVal approvalExists As Boolean, ByVal approvalSigner As String, ByVal approvalTimeStamp As Date)
            theLogger.Trace("Calling ApprovalInfo.New")
            Me.Field = approvalField
            Me.Exists = approvalExists
            Me.Signer = approvalSigner
            Me.TimeStamp = approvalTimeStamp
        End Sub

        Public Overrides Function ToString() As String
            theLogger.Trace("Calling ApprovalInfo.ToString")
            Return String.Format("[{0}] ApprovalExists: {1} Signer: '{2}' TimeStamp: '{3}'", Field, Exists, Signer, TimeStamp)
        End Function

        Public Overloads Function Equals(ByVal other As ApprovalInfo) As Boolean
            If ReferenceEquals(Nothing, other) Then Return False
            If ReferenceEquals(Me, other) Then Return True
            Return Equals(other._Field, _Field) AndAlso other._Exists.Equals(_Exists) AndAlso Equals(other._Signer, _Signer) AndAlso other._TimeStamp.Equals(_TimeStamp)
        End Function

        Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
            If ReferenceEquals(Nothing, obj) Then Return False
            If ReferenceEquals(Me, obj) Then Return True
            If Not Equals(obj.GetType(), GetType(ApprovalInfo)) Then Return False
            Return Equals(DirectCast(obj, ApprovalInfo))
        End Function

        Public Overrides Function GetHashCode() As Integer
            Dim hashCode As Long = 0
            If _Field IsNot Nothing Then hashCode = CInt(((hashCode * 397) Xor _Field.GetHashCode()) Mod Integer.MaxValue)
            hashCode = CInt((hashCode * 397) Xor _Exists.GetHashCode() Mod Integer.MaxValue)
            If _Signer IsNot Nothing Then hashCode = CInt(((hashCode * 397) Xor _Signer.GetHashCode()) Mod Integer.MaxValue)
            hashCode = CInt((hashCode * 397) Xor _TimeStamp.GetHashCode() Mod Integer.MaxValue)
            Return CInt(hashCode Mod Integer.MaxValue)
        End Function

        Public Shared Operator =(ByVal left As ApprovalInfo, ByVal right As ApprovalInfo) As Boolean
            Return Equals(left, right)
        End Operator

        Public Shared Operator <>(ByVal left As ApprovalInfo, ByVal right As ApprovalInfo) As Boolean
            Return Not Equals(left, right)
        End Operator
    End Class


(2) The GetHashCode method doesn't looks right. I think it should be:

    Public Overrides Function GetHashCode() As Integer
            Dim hashCode As Long = 0
            If Field IsNot Nothing Then hashCode = CLng(((hashCode * 397) Xor Field.GetHashCode()) Mod Integer.MaxValue)
            hashCode = CLng((hashCode * 397) Xor Exists.GetHashCode() Mod Integer.MaxValue)
            If Signer IsNot Nothing Then hashCode = CLng(((hashCode * 397) Xor Signer.GetHashCode()) Mod Integer.MaxValue)
            hashCode = CLng((hashCode * 397) Xor TimeStamp.GetHashCode() Mod Integer.MaxValue)
            Return CInt(hashCode Mod Integer.MaxValue)
        End Function

Viewing all articles
Browse latest Browse all 106942

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>