Instance ID?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!

Is there anyway to get a unique ID of an object, i.e. a primitive that
uniquely identifies an instance of an arbitrary type?

I don't want to explicitly associate an instance with some random
identifier, since I can't keep a reference to the object (it must be
"garbage-collectable").

Thanks in advance,
Nille
 
That is not sufficient, since that classifies the state of the object rather
than identifies the instance. This implies that the hashcode may not be
unique. I need an id that is unique.
 
I believe that a) at any given moment the system will not have two objects
with identical has codes and b) the hash code is not supposed to change over
the object's lifetime. Of course, since GetHashCode() is an overridable
method, someone could override it and make it non-unique in his
implementation, but that would break things like hashtables that rely on
hash code to find an element in the hash
 
Not sure what the problem is with assigning an id to each instance and not
keeping a reference to it. Can you expand on that?

Typically, if you want unique ids for instance of a certain type, you
declare a static variable on the type and increment it at the instance ctor
storing the latest value on the instance. That is your unique id for the
lifetime of the object.

Cheers
Daniel
 
That's only because I never stated my opinion on 3). In my mind I was wrong
on that one too <g>
 
You are getting there but not quite... in my mind there is a fourth
misconception that Angelos didn't list... I got that wrong as well... that
is 4 out of 3! <bg>

Cheers
Daniel
 
Back
Top