PC Review


Reply
Thread Tools Rate Thread

dictionay.contains does not call overloads overrides Equals method

 
 
Dot net work
Guest
Posts: n/a
 
      17th Oct 2004
Hello,

My simple code is here:

Public Class MyDictionary
Inherits System.Collections.DictionaryBase

Private Class MyElement
Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
End Function
End Class

Public Sub Add(<params>)
If Not dictionary.Contains(MyElementObject) Then
End If
End Sub
End Class


Do you know why the Equals method never gets called please?

Thank you, regards, dnw.
 
Reply With Quote
 
 
 
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      17th Oct 2004
Dnw,
Does MyElementObject represent a Value or a Key in the Dictionary?

Within DictionaryBase 'dictionary.Contains' "determines whether the
IDictionary contains an element with the specified key", hence if
MyElementObject represents a Value, Contains is always going to return
false...

If MyElementObject represents a Key, then it needs to override both
Object.Equals & Object.GetHashCode, as a Dictionary requires both to be
implemented.

For example:

Dim dictionary As New Hashtable
dictionary.Add(New KeyPair("A", "B"), "A value")
Debug.WriteLine(dictionary.Contains(New KeyPair("A", "B")),
"contains key pair")
Debug.WriteLine(dictionary.Contains("A value"), "contains a value")


Public Class KeyPair

Private ReadOnly m_value1 As String
Private ReadOnly m_value2 As String

Public Sub New(ByVal value1 As String, ByVal value2 As String)
m_value1 = value1
m_value2 = value2
End Sub

Public Overrides Function GetHashCode() As Integer
Return m_value1.GetHashCode() Xor m_value2.GetHashCode()
End Function

Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
If Not TypeOf obj Is KeyPair Then Return False
Dim other As KeyPair = DirectCast(obj, KeyPair)
Return m_value1 = other.m_value1 AndAlso m_value2 =
other.m_value2
End Function

End Class

Also its better that Equals & GetHashCode are based on readonly fields as I
have done above, otherwise you run the risk of returning inconsistent values
for Equals & GetHashCode which will cause even more confusion (for the
dictionary & you).

Hope this helps
Jay

"Dot net work" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> My simple code is here:
>
> Public Class MyDictionary
> Inherits System.Collections.DictionaryBase
>
> Private Class MyElement
> Public Overloads Overrides Function Equals(ByVal obj As Object) As
> Boolean
> End Function
> End Class
>
> Public Sub Add(<params>)
> If Not dictionary.Contains(MyElementObject) Then
> End If
> End Sub
> End Class
>
>
> Do you know why the Equals method never gets called please?
>
> Thank you, regards, dnw.



 
Reply With Quote
 
Dot net work
Guest
Posts: n/a
 
      18th Oct 2004
Awesome! You are a ninja programmer!

"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message news:<e9H#(E-Mail Removed)>...
> Dnw,
> Does MyElementObject represent a Value or a Key in the Dictionary?
>
> Within DictionaryBase 'dictionary.Contains' "determines whether the
> IDictionary contains an element with the specified key", hence if
> MyElementObject represents a Value, Contains is always going to return
> false...
>
> If MyElementObject represents a Key, then it needs to override both
> Object.Equals & Object.GetHashCode, as a Dictionary requires both to be
> implemented.
>
> For example:
>
> Dim dictionary As New Hashtable
> dictionary.Add(New KeyPair("A", "B"), "A value")
> Debug.WriteLine(dictionary.Contains(New KeyPair("A", "B")),
> "contains key pair")
> Debug.WriteLine(dictionary.Contains("A value"), "contains a value")
>
>
> Public Class KeyPair
>
> Private ReadOnly m_value1 As String
> Private ReadOnly m_value2 As String
>
> Public Sub New(ByVal value1 As String, ByVal value2 As String)
> m_value1 = value1
> m_value2 = value2
> End Sub
>
> Public Overrides Function GetHashCode() As Integer
> Return m_value1.GetHashCode() Xor m_value2.GetHashCode()
> End Function
>
> Public Overloads Overrides Function Equals(ByVal obj As Object) As
> Boolean
> If Not TypeOf obj Is KeyPair Then Return False
> Dim other As KeyPair = DirectCast(obj, KeyPair)
> Return m_value1 = other.m_value1 AndAlso m_value2 =
> other.m_value2
> End Function
>
> End Class
>
> Also its better that Equals & GetHashCode are based on readonly fields as I
> have done above, otherwise you run the risk of returning inconsistent values
> for Equals & GetHashCode which will cause even more confusion (for the
> dictionary & you).
>
> Hope this helps
> Jay
>
> "Dot net work" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hello,
> >
> > My simple code is here:
> >
> > Public Class MyDictionary
> > Inherits System.Collections.DictionaryBase
> >
> > Private Class MyElement
> > Public Overloads Overrides Function Equals(ByVal obj As Object) As
> > Boolean
> > End Function
> > End Class
> >
> > Public Sub Add(<params>)
> > If Not dictionary.Contains(MyElementObject) Then
> > End If
> > End Sub
> > End Class
> >
> >
> > Do you know why the Equals method never gets called please?
> >
> > Thank you, regards, dnw.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
implicitly handle operator == when class overrides Equals method Steve Richter Microsoft C# .NET 5 18th Feb 2008 08:56 PM
Interface + derived method (via Overloads) problem Lothar Behrens Microsoft VB .NET 5 7th Jan 2008 04:36 PM
difference between 'overrides' and 'overloads' in class? André Microsoft VB .NET 12 18th Mar 2007 11:01 AM
Overrides Controls Method Kalim Julia Microsoft VB .NET 2 27th Jan 2006 02:24 AM
Overloads Overrides Atif Microsoft VB .NET 8 27th Sep 2004 09:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:09 AM.