473,385 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

dictionay.contains does not call overloads overrides Equals method

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.
Nov 21 '05 #1
2 2630
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" <do***@hotmail.com> wrote in message
news:77**************************@posting.google.c om...
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.

Nov 21 '05 #2
Awesome! You are a ninja programmer!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:<e9**************@TK2MSFTNGP14.phx.gbl>...
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" <do***@hotmail.com> wrote in message
news:77**************************@posting.google.c om...
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.

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joăo Santa Bárbara | last post by:
Hi all i have a class that i have made and i made an Typeconverter as well, and my problem is the code genereated in my form is Dim ClassTest1 As TestApplication.ClassTest = New...
0
by: Joăo Santa Bárbara | last post by:
Hi all i have a class that i have made and i made an Typeconverter as well, and my problem is the code genereated in my form is Dim ClassTest1 As TestApplication.ClassTest = New...
2
by: Piotr Szukalski | last post by:
Hi! I have trouble with 'Contains' method in ListViewItemCollection class - it seems like it nevers calls 'Equals' method of class inherited from ListViewItem... I've found that ListViewItem...
5
by: matt tagliaferri | last post by:
I'm implementing a typed collection class that can't include duplicates. The Add method of the collection class is as follows: Public Function Add(ByVal o As TeamListEntry) As Integer If...
12
by: Lee Silver | last post by:
In a base class I have the following 2 declarations: Overridable Sub Remove(ByVal wIndex As Integer) and Overridable Sub Remove(ByVal wValue As Object) In an immediately derived class I...
10
by: Atif | last post by:
Hi I am here to solve a small confusion i have in "Overloads Overrides". "Overloading" says that the method's name should be same while no. of parameters and/or their datatypes should be changed...
3
by: Ken | last post by:
How can I have my WinForm return something to the caller so it knows which button is clicked? The following is calling a system message box, just want to do the samething with my own winform....
6
by: Philip Warner | last post by:
I have a set of classes: C (base) C1 (inherits C) C2 (inherits C) ... Cn (inherits C) (the C1..Cn also have subclasses, but thats not really relevant to the question)
3
by: JB | last post by:
I've created a generic of type List<T> where T is a custom class. I need to use the List<T>.Contains method. I know I need to implement the IEqualityComparer but I can't seem to get the Contains...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.