How to override GetHashCode() method to get hash code for the current object in VB.NET

2 Answers

0 votes
Public Class Point
    Private x As Integer
    Private y As Integer

    Public Sub New(x As Integer, y As Integer)
        Me.x = x
        Me.y = y
    End Sub

    Public Overrides Function GetHashCode() As Integer
        Return Tuple.Create(x, y).GetHashCode()
    End Function
End Class

Module Module1

    Sub Main()
        Dim pt As New Point(7, 3)
        Console.WriteLine(pt.GetHashCode())

        pt = New Point(3, 7)
        Console.WriteLine(pt.GetHashCode())
    End Sub

End Module

' run:
' 
' 228
' 100

 



answered Apr 24, 2016 by avibootz
0 votes
Public Class Point
    Private x As Integer
    Private y As Integer

    Public Sub New(x As Integer, y As Integer)
        Me.x = x
        Me.y = y
    End Sub

    Public Overrides Function GetHashCode() As Integer
        Return x Xor y
    End Function
End Class

Module Module1

    Sub Main()
        Dim pt As New Point(7, 3)
        Console.WriteLine(pt.GetHashCode())

        pt = New Point(3, 7)
        Console.WriteLine(pt.GetHashCode())
    End Sub

End Module

' run:
' 
' 4
' 4

 



answered Apr 24, 2016 by avibootz

Related questions

1 answer 132 views
1 answer 123 views
1 answer 145 views
1 answer 113 views
...