H o m e
T h e    O b j e c t    C l a s s ’    E q u a l    a n d    G e t H a s h C o d e    M e t h o d s

 

By Michael McIntyre

mikemc@getdotnetcode.com

 

This article discusses the Object class’ overloadable Equals and Overrideable GetHashCode methods.  The default implementations of these two methods in the Object class don’t do anything very useful. It is left to you to overload the Equals method and override the GetHashCode to make them useful in classes you derive from the Object class. This article explains why and provides some ‘how to’ information with sample code.

 

Here are two code examples.  Explanations follow the examples.

 

Example Code – Student Class

 

Public Class Student

 

    ' StudentID field.

    Private _StudentID As Integer

    ' StudentID property.

    Public Property StudentID() As Integer

        Get

            Return _StudentID

        End Get

        Set(ByVal Value As Integer)

            _StudentID = Value

        End Set

    End Property

 

    ' Name field.

    Private _Name As String

    ' Name property.

    Public Property Name() As String

        Get

            Return _Name

        End Get

        Set(ByVal Value As String)

            _Name = Value

        End Set

    End Property

 

    ' Overload the Object class' Equals method.

    Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean

        ' The Student database table's primary key consists of one column,

        '   the StudentID column. StudentID is immutable and unique data.

        ' Use this object's StudentID data to test whether the object passed into

        '   this method is equal to this student object.

        Try

            Return MyBase.Equals(obj) And (CType(obj, Student).StudentID = Me.StudentID)

        Catch

            Return False

        End Try

    End Function

 

    ' Override the Object class' GetHashCode method.

    Public Overrides Function GetHashCode() As Integer

        '  Use the same object data we used when we overloaded the Equals method.

        '  Since StudentID is immutable, unique, and an integer value

        '    we can return it 'as it' as the hash code for this Student object.

        '    (a hash code is an integer value).

        Return _StudentID

    End Function

 

End Class

 

Example Code – WarehouseBin Class

 

Public Class WarehouseBin

 

    ' WarehouseID field.

    Private _WarehouseID As String

    ' WarehouseID property.

    Public Property WarehouseID() As String

        Get

            Return _WarehouseID

        End Get

        Set(ByVal Value As String)

            _WarehouseID = Value

        End Set

    End Property

 

    ' BinID field.

    Private _BinID As Integer

    ' BinID property.

    Public Property BinID() As Integer

        Get

            Return _BinID

        End Get

        Set(ByVal Value As Integer)

            _BinID = Value

        End Set

    End Property

 

    ' Overload the Object class' Equals method.

    Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean

        ' The database table WarehouseBin's primary key consists of

        '   two columns, the WarehouseID and the BinID columns.

        ' Combined together, WarehouseID and BinID is immutable and unique data.

        ' Use the object's WarehouseID data and BinID data to test whether the

        '   object passed into this method is equal to this WarehouseBin object.

        Try

            Return MyBase.Equals(obj) And CType(obj, WarehouseBin).WarehouseID = _

                Me.WarehouseID And (CType(obj, WarehouseBin).BinID = Me.BinID)

        Catch

            Return False

        End Try

    End Function

 

    ' Override the Object class' GetHashCode method.

    Public Overrides Function GetHashCode() As Integer

        '  Use the same object data we used when we overloaded the Equals method

        '    to generate a unique hash code.

        '  Since WarehouseID is a string and BinID is a integer we

        '    convert the BinID to string, append it to WarehouseID,

        '    call GetHashCode on the result and return the result as

        '    this WarehouseBin object's hash code.

        Return CInt((Me._WarehouseID & Me._BinID.ToString).GetHashCode)    End Function

End Class

 

Equals Method

 

The purpose of the Object class’s Equals method is to test for equality between the value of an object and another object.

 

The Object class’ Equals method’s default implementation checks two variables to see if they both point to the same object in memory.  In other words, the Equals method checks the location in memory each variable references and if it’s the same memory location returns True.

 

Most of the time we want our classes to check for equality between two objects based on the data they contain, not their location in memory.  For example, when comparing two BankAcount objects we probably want to determine they are equal if they have the same unique account number.

 

Overload the Equals method when you author a class so that immutable and unique data in an object instantiated from your class is used to check the equality of one instance of your class to another.

 

A good example of immutable and unique data is the database primary key associated with entities such as a students, warehouse bins, and employees.

 

A primary key may consist of one database column or more. 

 

If the primary key for a database table named Student consists of one column, the StudentID column, then StudentID is the immutable and unique data to use in a Student class’ Equals method.

 

See the Example Code – Student Class above.

 

 

 

GetHashCode Method

 

The purpose of the Object class’ GetHashCode method is to get a hash code suitable for use in hashing algorithms and data structures like a hash table.

 

The Object class’ GetHashCode method’s default implementation returns a unique index for each instance, determined by the runtime – not by data in the object.

 

A class that overloads the Equals method should always override the GetHashCode method. Two objects that are equal should generate the same hash code. The easiest way to implement this is to use the same object data your use when you overloaded the Equals method to generate a unique hash code.

 

Override the GetHashCode method when you author a class by using the same data you used to overload your Equals method to generate an object’s hash code. 

 

If the primary key for a database table name WarehouseBin consists of two columns, the WarehouseID and BinID columns, then WarehouseID plus BinID is the immutable and unique data to use in a WarhouseBin class’ Equals method.

 

See the Example Code – WarehouseBin Class above.

 

 

Copyright © 2001-2005 aZ Software Developers. All rights reserved.