473,383 Members | 1,818 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,383 software developers and data experts.

Object identity

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 Object.ReferenceEquals() shared
method use for its comparison?

In Python, a unique identifier can be obtained with: id(object)

Thanks,
Daniel Klein
Nov 20 '05 #1
6 1238
I was asking a similar question, here it is and a good response I got from
it.

Regards - OHM

-----------------
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.
---------------

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.

--
Daniel Klein wrote:
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 Object.ReferenceEquals() shared
method use for its comparison?

In Python, a unique identifier can be obtained with: id(object)

Thanks,
Daniel Klein


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com
Nov 20 '05 #2
Daniel,
Can the Object GetHashCode() method be used to obtain a unique
identifier for an instance of an object?
No

To put this another way, what does the Object.ReferenceEquals() shared
method use for its comparison?


It does something like

Return (obj1 Is obj2)

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #3
Daniel,
Can the Object GetHashCode() method be used to obtain a unique
identifier for an instance of an object? No, OHM gave all the reasons (I know of) that it cannot.
To put this another way, what does the Object.ReferenceEquals() shared
method use for its comparison? The actual "address" of the object on the heap.

Remember that reference type variables (aka Objects) are actually a
reference (pointer/memory address) to the object data itself on the heap,
Object.ReferenceEquals compares these two pointers.
In Python, a unique identifier can be obtained with: id(object) After you get this unique identifier, what are you doing with it?

I find for most reference types the object reference itself is sufficient
for a unique identifier, normally when I need a different unique identifier,
is when I am mapping the object back to a Database or something "concrete"
that needs to be logically persisted as opposed to physically persisted (an
ASP.NET session)...

Hope this helps
Jay

"Daniel Klein" <da*****@aracnet.com> wrote in message
news:0n********************************@4ax.com... 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 Object.ReferenceEquals() shared
method use for its comparison?

In Python, a unique identifier can be obtained with: id(object)

Thanks,
Daniel Klein

Nov 20 '05 #4
On Sun, 11 Jan 2004 12:52:58 -0600, "Jay B. Harlow [MVP - Outlook]"
<Ja************@msn.com> wrote:
In Python, a unique identifier can be obtained with: id(object)

After you get this unique identifier, what are you doing with it?


The Python application I'm converting from is using it as a temporary
database key. It's really no problem changing this to something like a
sequential number.

Thanks for the feedback,

Daniel Klein
Nov 20 '05 #5
Daniel,
I would recommend either a sequential number of a System.GUID. The GUID
would significantly reduce the chance of a key conflict.

I don't know off hand of any built-in support for Python's id function.

Hope this helps
Jay

"Daniel Klein" <da*****@aracnet.com> wrote in message
news:1h********************************@4ax.com...
On Sun, 11 Jan 2004 12:52:58 -0600, "Jay B. Harlow [MVP - Outlook]"
<Ja************@msn.com> wrote:
In Python, a unique identifier can be obtained with: id(object)

After you get this unique identifier, what are you doing with it?


The Python application I'm converting from is using it as a temporary
database key. It's really no problem changing this to something like a
sequential number.

Thanks for the feedback,

Daniel Klein

Nov 20 '05 #6
In article <1h********************************@4ax.com>, Daniel Klein wrote:
On Sun, 11 Jan 2004 12:52:58 -0600, "Jay B. Harlow [MVP - Outlook]"
<Ja************@msn.com> wrote:
In Python, a unique identifier can be obtained with: id(object)

After you get this unique identifier, what are you doing with it?


The Python application I'm converting from is using it as a temporary
database key. It's really no problem changing this to something like a
sequential number.

Thanks for the feedback,

Daniel Klein


Question?... Does it really need to be converted from python?
ActiveState has a python compiler for .NET (perl as well). I haven't
tried it, but you may want to check it out :) Might save you a lot of
trouble.

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #7

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

Similar topics

1
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...
1
by: SR | last post by:
Hi This is because the ASPNet worker process runs in a user mode who does not have launch permissions for that component. either make the aspnet worker process user identity as an...
3
by: vincentw56 | last post by:
This is the first time I have tried this. What we have is a com object the generates keys. A developer here wrote a .Net wrapper in C# for this com object. I am trying to call her dll wrapper...
5
by: gregfocker | last post by:
OK, the ProgID is correct. The .dll is registered. This is a permission issue. I can create objects when I disable anonymous access on the Web site in IIS (Win XP Pro). And I've given...
1
by: Ben Pryhoda | last post by:
I have already used a third party com object in my code, and it went easily. Add the reference, call the methods, and get my results. But I am integrating a legacy com object and I follow the...
2
by: k | last post by:
Access is denied. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it...
11
by: Amit Agarwal | last post by:
where is the place to attach pricipall object to identity. global file and is it necessary to attach each time user roles to principal object.. amit --- Outgoing mail is certified Virus...
2
by: CodeCowboy | last post by:
I'm sure some of you have done this before and I've been perusing through the forum trying to find some uncomplicated solution. I am trying to extend the existing user.identity object. I would...
11
by: Stephan Keil | last post by:
Hi all, I am a novice with .NET and I am wondering if there is something like an "identity value" of an object. I mean something like the object's address in C++ or C, i.e. a fixed unique value...
2
by: Yasin Cepeci | last post by:
Server Error in '/' Application. -------------------------------------------------------------------------------- Access to the path...
0
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...
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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.