Hi,
I need to produce an ID number that uniquely identifies a given object
instance.
(If the object is a boxed value, well, it won't really matter, but for
_shared_ reference-types I really need a unique ID)
What's the most efficient way to do this?
I'm thinking of using a Hashtable using the objects themselves as keys and
increasing integers as IDs.
Many of the reference-types that participate have "Equivalenc e" semantics
for equality testing, meaning that they override .Equals() to return True
when 2 instances (same or not) have the same value; which implies that they
also override GetHashCode() so I have to use my own hash code provider; but
that's not a problem.
Any idea about an alternative approach?
A related question: is there any way to determine if an objet is shared
(more than 1 reference to it)?
TIA
Fernando Cacciola 5 11308
Fernando Cacciola <fe************ ***@hotmail.com > wrote: I need to produce an ID number that uniquely identifies a given object instance.
(If the object is a boxed value, well, it won't really matter, but for _shared_ reference-types I really need a unique ID)
What's the most efficient way to do this?
I'm thinking of using a Hashtable using the objects themselves as keys and increasing integers as IDs.
Many of the reference-types that participate have "Equivalenc e" semantics for equality testing, meaning that they override .Equals() to return True when 2 instances (same or not) have the same value; which implies that they also override GetHashCode() so I have to use my own hash code provider; but that's not a problem.
Any idea about an alternative approach?
I don't think you need your own hash code provider - I think you need
your own IComparer which just does reference equality. I would have
thought that using the object's own hash code would be the right way to
go.
A related question: is there any way to determine if an objet is shared (more than 1 reference to it)?
Not that I know of. Depending on what you're using it for,
WeakReference might be helpful though.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Fernando Cacciola" <fe************ ***@hotmail.com > wrote in message
news:ux******** ******@TK2MSFTN GP10.phx.gbl... Hi,
I need to produce an ID number that uniquely identifies a given object instance.
(If the object is a boxed value, well, it won't really matter, but for _shared_ reference-types I really need a unique ID)
What's the most efficient way to do this?
I'm thinking of using a Hashtable using the objects themselves as keys and increasing integers as IDs.
Many of the reference-types that participate have "Equivalenc e" semantics for equality testing, meaning that they override .Equals() to return True when 2 instances (same or not) have the same value; which implies that they also override GetHashCode() so I have to use my own hash code provider; but that's not a problem.
Any idea about an alternative approach?
You can supply your own IComparer for use in the hash table and override the
default comparison.
class ReferenceCompar er : System.Collecti ons.IComparer
{
public int Compare(object a, object b)
{
return object.Referenc eEquals(a,b)?0: 1;
}
}
static void main(string[] args)
{
Hashtable t = new Hashtable(null, new ReferenceCompar er());
string s2 = "a";
s2 = s2 + "bc";
t.Add(s2,2);
Console.WriteLi ne(t.ContainsKe y("abc"));
//false s2 is a different string than the interned literal "abc"
string s1 = "abc";
t.Add(s1,1);
Console.WriteLi ne(t.ContainsKe y("abc"));
//true, but only because "abc" is interned
Console.WriteLi ne(t.ContainsKe y("abc ".Trim()));
//false
string s3 = s2;
t.Add(s3,3);
//fails because s3 references the same object as s2
}
A related question: is there any way to determine if an objet is shared (more than 1 reference to it)?
No, not really. This is only known by the garbage collector during a
garbage collection. So you could get a WeakReference to the object, release
your strong reference, run a complete garbage collection, and check if it's
still alive. But that's hardly practical.
David
Fernando,
There is no way to determine if an object has more than one reference to
it.
As for doing a comparison, I think that overriding Equals is a bad thing
in general. It's the type of thing that should be implemented on an
interface, I think.
For what you want, I think that you should cast both objects to object,
and then use the == comparison operator, or use the static ReferenceEquals
method on the Object class.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Fernando Cacciola" <fe************ ***@hotmail.com > wrote in message
news:ux******** ******@TK2MSFTN GP10.phx.gbl... Hi,
I need to produce an ID number that uniquely identifies a given object instance.
(If the object is a boxed value, well, it won't really matter, but for _shared_ reference-types I really need a unique ID)
What's the most efficient way to do this?
I'm thinking of using a Hashtable using the objects themselves as keys and increasing integers as IDs.
Many of the reference-types that participate have "Equivalenc e" semantics for equality testing, meaning that they override .Equals() to return True when 2 instances (same or not) have the same value; which implies that they also override GetHashCode() so I have to use my own hash code provider; but that's not a problem.
Any idea about an alternative approach?
A related question: is there any way to determine if an objet is shared (more than 1 reference to it)?
TIA
Fernando Cacciola
"Jon Skeet [C# MVP]" <sk***@pobox.co m> escribió en el mensaje
news:MP******** *************** @msnews.microso ft.com... Fernando Cacciola <fe************ ***@hotmail.com > wrote: I need to produce an ID number that uniquely identifies a given object instance.
(If the object is a boxed value, well, it won't really matter, but for _shared_ reference-types I really need a unique ID)
What's the most efficient way to do this?
I'm thinking of using a Hashtable using the objects themselves as keys and increasing integers as IDs.
Many of the reference-types that participate have "Equivalenc e" semantics for equality testing, meaning that they override .Equals() to return True when 2 instances (same or not) have the same value; which implies that they also override GetHashCode() so I have to use my own hash code provider; but that's not a problem.
Any idea about an alternative approach? I don't think you need your own hash code provider - I think you need your own IComparer which just does reference equality. I would have thought that using the object's own hash code would be the right way to go.
:-) This is exactly what I ended up doing after I tried out some code. A related question: is there any way to determine if an objet is shared (more than 1 reference to it)? Not that I know of. Depending on what you're using it for, WeakReference might be helpful though.
Oh, I'll check this out. Txs!
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
Fernando Cacciola
SciSoft
"David Browne" <davidbaxterbro wne no potted me**@hotmail.co m> escribió en el
mensaje news:%2******** **********@TK2M SFTNGP11.phx.gb l... "Fernando Cacciola" <fe************ ***@hotmail.com > wrote in message news:ux******** ******@TK2MSFTN GP10.phx.gbl... Hi,
I need to produce an ID number that uniquely identifies a given object instance.
(If the object is a boxed value, well, it won't really matter, but for _shared_ reference-types I really need a unique ID)
What's the most efficient way to do this?
I'm thinking of using a Hashtable using the objects themselves as keys and increasing integers as IDs.
Many of the reference-types that participate have "Equivalenc e" semantics for equality testing, meaning that they override .Equals() to return True when 2 instances (same or not) have the same value; which implies that they also override GetHashCode() so I have to use my own hash code provider; but that's not a problem.
Any idea about an alternative approach? You can supply your own IComparer for use in the hash table and override the default comparison.
class ReferenceCompar er : System.Collecti ons.IComparer { public int Compare(object a, object b) { return object.Referenc eEquals(a,b)?0: 1; } }
Yes, this is what I finally did a little after I posted.
(Except that I just used operator== since operators works by overloading,
and in this case the arguments are of type object so it always performs
reference equality.
static void main(string[] args) { Hashtable t = new Hashtable(null, new ReferenceCompar er());
Ha, didn't think of suppling null as the HashCodeProvide r...
Good point! A related question: is there any way to determine if an objet is shared (more than 1 reference to it)?
No, not really. This is only known by the garbage collector during a garbage collection. So you could get a WeakReference to the object, release your strong reference, run a complete garbage collection, and check if it's still alive. But that's hardly practical.
Hmm, yes, I see...
I essentially just thought about skipping the unique ID althogether for
objects which are not shared (because if they are not shared, a unique ID is
not important for my application).
But given the above it would be a mis-optimization :-)
Fernando Cacciola
SciSoft
David This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Puvendran Selvaratnam |
last post by:
Hi,
First of all my apologies if you have seen this mail already but I am
re-sending as there were some initial problems.
This query is related to defining indexes to be unique or not and
consequences thereof.
Some documented facts that I am aware of include
|
by: Naran Hirani |
last post by:
Hi Guys and Gals,
Please can u help me with this little problem.
I am collecting a set of values from a web form and putting them into a
JS array object.
I now want to check/ensure that there or no repeated values in this array.
I can think of many ways to do this using one or many loops but was just
wondering if there was
any other clever way of doing this using string functions or the RegExp
|
by: Anna |
last post by:
Hi all.
I have a form with a few text boxes which need to be validated client-side.
The validation is: check that every single text box has a unique string value.
I.e., I need to check that there are no two textboxes that both contain,
for example, the string 'hello'.
Is there an efficient way to do this kind of validation in javascript?
Thank you very much for your help.
|
by: Doug |
last post by:
Is there any harm in passing an object into a method with the 'ref'
keyword if the object is already a reference variable? If not, is
there any benefit?
|
by: Dirk Declercq |
last post by:
Hi,
Is it possible in Xml to enfore the uniqueness of an element based on his
attribute value.
Say I have this schema :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.egemin.com/Epia/StringResources"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
| |
by: Mr. Almenares |
last post by:
Hello:
I’m trying to do a schema with recurrent structure for a Book like a Node
can have many Nodes inside or One leave. So, the leaves have an attribute
that is Identifier. My goal is define Uniqueness that guarantees to the
attribute Identifier his uniqueness. That I don’t know the depth of levels I
have to put in the xpath attribute of the selector something like this
TOC/descendant::Tree/Data, but this is not allow. How I can...
|
by: Alan Little |
last post by:
I have affiliates submitting batches of anywhere from 10 to several
hundred orders. Each order in the batch must include an order ID,
originated by the affiliate, which must be unique across all orders in
all batches ever submitted by that affiliate. I'm trying to figure out
the most efficient way to check the uniqueness of the order ID.
Order data is being submitted to Zen Cart, and also stored in custom
tables. I have created a unique...
|
by: gg |
last post by:
is it possible to have a complex type with attribute pointing to another
member
of the complex type?
for example, I tried
<xsd:complexType name="RegexTypes" />
<xsd:sequence>
<xsd:element name="regexName" type="xsd:Name" />
|
by: Astley Le Jasper |
last post by:
Sorry for the numpty question ...
How do you find the reference name of an object?
So if i have this
bob = modulename.objectname()
how do i find that the name is 'bob'
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |