RSS

Compare class objects with IEquatable

13 Feb

Last week I got a problem when using List.Contains() method to see if a specific object is in the list. For example:
The class definition:

Public Class Book

    Private _title As String
    Public Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    Private _edition As String
    Public Property Edition() As String
        Get
            Return _edition
        End Get
        Set(ByVal value As String)
            _edition = value
        End Set
    End Property

End Class


Main:

' Default list of Book
Dim bookList As New List(Of Book)
bookList.Add(New Book("Marry Hotter", "1st"))
bookList.Add(New Book("Badman", "1st"))

' New list of Book
Dim newBookList As New List(Of Book)
newBookList.Add(New Book("Marry Hotter", "1st"))
newBookList.Add(New Book("Marry Hotter", "2nd"))
newBookList.Add(New Book("Drumming For Dummies", "1st"))

' Add new Book instance into the bookList if the book is not in the list
' Books with different title and edition are regarded as different books
For Each newBook In newBookList
    If Not bookList.Contains(newBook) Then
        bookList.Add(newBook)
    End If
Next

' books in bookList after execution:
' "Marry Hotter", "1st"
' "Badman", "1st"
' "Drumming For Dummies", "1st"

The first Book instance of newBookList (the “Marry Hotter”, “1st”) is not added to the bookList which is correct, but the second one isn’t either. It seems the method only compares the 1st property of the class. In order to compare the Book instances as expected, I have to implement IEquatable in the class definition.

Here is the refined class definition:

Public Class Book
    Implements IEquatable(Of Book)

    Private _title As String
    Public Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    Private _edition As String
    Public Property Edition() As String
        Get
            Return _edition
        End Get
        Set(ByVal value As String)
            _edition = value
        End Set
    End Property

    Public Overloads Function Equals(ByVal other As Book) As Boolean Implements IEquatable(Of Book).Equals
        Return other.Title= Me.Title AndAlso other.Edition= Me.Edition
    End Function

End Class

Please feel free to make any suggestion!

Advertisements
 
Leave a comment

Posted by on February 13, 2012 in .NET, VB

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: