The help for the .NET Framework Class Library tells us that the Object.GetHashCode() Method does not guarantee uniqueness' or consistency and that overriding this and the Equals method is a good idea.
It also tells us that using the XOR functions on two or more Fields or Properties is an acceptable way to achieve this.
What do you guys think is the best approach to this given that generating a hashcode should be fast as well as consistant.
--
Best Regards - OHM
O_H_M{at}BTInternet{dot}com 2 2347
OHM,
When I'm defining classes that I want to be "HashTable friendly" I do both
of what you quoted, the general template I follow is:
Public NotInheritable Class KeyPair
Private ReadOnly m_key1, m_key2 As Integer
Public Sub New(ByVal key1 As Integer, ByVal key2 As Integer)
m_key1 = key1
m_key2 = key2
End Sub
Public Overrides Function GetHashCode() As Integer
Return m_key1.GetHashCode() Xor m_key2.GetHashCode()
End Function
Public Overloads Function Equals(ByVal other As KeyPair) As Boolean
Return m_key1 = other.m_key1 AndAlso m_key2 = other.m_key2
End Function
Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
If TypeOf obj Is KeyPair Then
Return Me.Equals(DirectCast(obj, KeyPair))
Else
Return False
End If
End Function
End Class
Note that there may be more "key" fields, and there may be non "key" fields
also. Normally I make the "key" fields immutable (ReadOnly) to ensure that
the HashCode does not change causing problems for the HashTable itself. In a
couple cases I notify the container (HashTable) that a "key" is changing, so
that it can remove the old key and add the new key.
Remember that the HashCode gives the HashTable a slot to put the item into,
that the HashTable then uses the Equals function to see if the item is
already in that slot. In other words a single slot can have multiple items.
Also there are other "requirements" that make a good hash code, however I'm
taking it on faith that the primitive types return good hash codes. By
making my types build upon the primitive types, my types should also have
fairly good hash codes...
Hope this helps
Jay
"One Handed Man [ OHM# ]" <O_H_M{at}BTInternet{dot}com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
The help for the .NET Framework Class Library tells us that the
Object.GetHashCode() Method does not guarantee uniqueness' or consistency
and that overriding this and the Equals method is a good idea.
It also tells us that using the XOR functions on two or more Fields or
Properties is an acceptable way to achieve this.
What do you guys think is the best approach to this given that generating a
hashcode should be fast as well as consistant.
--
Best Regards - OHM
O_H_M{at}BTInternet{dot}com
Its a a little bit of a thorny subject. Many thanks for your reply which is
excellent as allways.
Regards - OHM
---------------
Jay B. Harlow [MVP - Outlook] wrote: OHM, When I'm defining classes that I want to be "HashTable friendly" I do both of what you quoted, the general template I follow is:
Public NotInheritable Class KeyPair
Private ReadOnly m_key1, m_key2 As Integer
Public Sub New(ByVal key1 As Integer, ByVal key2 As Integer) m_key1 = key1 m_key2 = key2 End Sub
Public Overrides Function GetHashCode() As Integer Return m_key1.GetHashCode() Xor m_key2.GetHashCode() End Function
Public Overloads Function Equals(ByVal other As KeyPair) As Boolean Return m_key1 = other.m_key1 AndAlso m_key2 = other.m_key2 End Function
Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean If TypeOf obj Is KeyPair Then Return Me.Equals(DirectCast(obj, KeyPair)) Else Return False End If End Function
End Class
Note that there may be more "key" fields, and there may be non "key" fields also. Normally I make the "key" fields immutable (ReadOnly) to ensure that the HashCode does not change causing problems for the HashTable itself. In a couple cases I notify the container (HashTable) that a "key" is changing, so that it can remove the old key and add the new key.
Remember that the HashCode gives the HashTable a slot to put the item into, that the HashTable then uses the Equals function to see if the item is already in that slot. In other words a single slot can have multiple items. Also there are other "requirements" that make a good hash code, however I'm taking it on faith that the primitive types return good hash codes. By making my types build upon the primitive types, my types should also have fairly good hash codes...
Hope this helps Jay
"One Handed Man [ OHM# ]" <O_H_M{at}BTInternet{dot}com> wrote in message news:%2****************@tk2msftngp13.phx.gbl... The help for the .NET Framework Class Library tells us that the Object.GetHashCode() Method does not guarantee uniqueness' or consistency and that overriding this and the Equals method is a good idea.
It also tells us that using the XOR functions on two or more Fields or Properties is an acceptable way to achieve this.
What do you guys think is the best approach to this given that generating a hashcode should be fast as well as consistant.
--
Best Regards - OHM
O_H_M{at}BTInternet{dot}com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Clemens Hoffmann |
last post by:
Hello,
i have a nasty problem with object identity in
hash tables. I try to use different objects as keys.
It failed bacause i cannot identify object propperly.
Different objects with the same...
|
by: Bijay Kumar |
last post by:
Hi Guys,
I was going through the source code of Object.cs in rotor. What I found is
Equals() implemented as follows:
public extern virtual bool Equals(Object obj);
What I don't...
|
by: Bijay Kumar |
last post by:
Hi Guys,
I was going through the source code of Object class (Object.cs in rotor). What I found is
Equals() implemented as follows:
public extern virtual bool Equals(Object obj);
What...
|
by: Lars Moastuen |
last post by:
Hi!
I'm currently playing around with a project where I need to know if an
object (any object) has been altered since last check. I need this to know
when an object needs to be saved... My idea...
|
by: Clint Herron |
last post by:
Howdy!
I posted this question on CSharpCorner.com, but then realized I should
probably post it on a more active newsgroup. This will be my only cross-post.
I'm creating a game engine, and...
|
by: Arjen |
last post by:
Hi,
I have a class with some attributes. For example class person with name as
attribute.
I have add multiple persons in an arraylist.
Now I need a function to get/find a person by the name...
|
by: Daniel Klein |
last post by:
A simple question hopefully...
Can the Object GetHashCode() method be used to obtain a unique
identifier for an instance of an object?
To put this another way, what does the...
|
by: Sam |
last post by:
Hi Everyone,
I have a stucture below stored in an arraylist and I want to check user's
input (point x,y) to make sure there is no duplicate point x,y entered
(string label can be duplicated). Is...
|
by: Jesper |
last post by:
Hi,
I've made a class with the following operator overloads listed below.
However, If I test a 'pointer' for an instace, I get an exception that the
object is not set to an insstance of an...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
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...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
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...
| |