473,327 Members | 2,081 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,327 software developers and data experts.

Creating a list of objects with unique identifiers

I need some opionions on an issue here...
Suppose I want to keep a collection of objects, each need to be
uniquely identified by a number. This number has no meaning as long as
it's unique, so it should be automatically generated for each new
object. One more requirement is keeping the objects in the order in
which they were inserted, not sorted by their ID.
Considering the following options of implementation, which would be the
best approach (and if I'm missing something, please do advise...):

1. Keeping the objects in a dictionary with the key being the unique
identifier. This has the disadvantage of not being able to read the ID
directly from the object, only from it's parent (you can't use
myobject.ID unless you keep the ID in the object as well, which is
duplicate data). This also requires the "user" of the dictionary to
iterate through all objects and generate a new unique ID for each
object added.

2. Create an ID field for the object, and then keep the objects in a
List. Still requires the list user to generate an ID.

3. A more database-like approach: Create an ID field for the object,
and then create a new collection that implements IList<T>. When Add()
is called on the collection, the implementation will generate a new
unique ID and set the object's ID field. This is a similar approach to
adding a new record to a table with an identity column in a DB.

Thoughts and suggestions would be greatly appreciated.
Danny

Oct 31 '06 #1
1 6250
PS

"Danny Liberty" <dl******@gmail.comwrote in message
news:11**********************@e64g2000cwd.googlegr oups.com...
>I need some opionions on an issue here...
Suppose I want to keep a collection of objects, each need to be
uniquely identified by a number. This number has no meaning as long as
it's unique, so it should be automatically generated for each new
object. One more requirement is keeping the objects in the order in
which they were inserted, not sorted by their ID.
Considering the following options of implementation, which would be the
best approach (and if I'm missing something, please do advise...):

1. Keeping the objects in a dictionary with the key being the unique
identifier. This has the disadvantage of not being able to read the ID
directly from the object, only from it's parent (you can't use
myobject.ID unless you keep the ID in the object as well, which is
duplicate data). This also requires the "user" of the dictionary to
iterate through all objects and generate a new unique ID for each
object added.

2. Create an ID field for the object, and then keep the objects in a
List. Still requires the list user to generate an ID.

3. A more database-like approach: Create an ID field for the object,
and then create a new collection that implements IList<T>. When Add()
is called on the collection, the implementation will generate a new
unique ID and set the object's ID field. This is a similar approach to
adding a new record to a table with an identity column in a DB.

Thoughts and suggestions would be greatly appreciated.
Look at KeyedCollection<TKey, TItem>. It pretty much covers all 3 items you
mentioned. You will need to have an ID property that is assigned an unique
value when the object is contructed.

PS

Oct 31 '06 #2

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

Similar topics

1
by: Alejandro Narancio | last post by:
Hello my name is Alejandro and i've a question. I need to make a functions with this characteristics: - params: an array of strings with the names of components in a page (for example, some...
20
by: William Stacey [MVP] | last post by:
int list = {1,2,3,4,5,6}; Function to randomize the list? Cheers! -- William Stacey, MVP
3
by: Kilroy Programmer | last post by:
Is there a way to store a unique numeric identifier (say, for example, an int) into a TreeNode, so that when the TreeNode is checked (since CheckBoxes is enabled) the eventhandler AfterCheck() can...
1
by: Little | last post by:
Could someone help me figure out how to put my project together. I can't get my mind wrapped around the creation of the 4 double Linked Lists. Thank your for your insight. 1. Create 4 double...
5
by: Greg Corradini | last post by:
Hello All, I'm attempting to create multiple dictionaries at once, each with unique variable names. The number of dictionaries i need to create depends on the length of a list, which was returned...
6
by: Henrik Goldman | last post by:
Hello, I have a dataset which consist of a string username and string hostname as a key and then an integer representing a count as the matching "second" value in a pair. So far I've used...
0
by: jappenzeller | last post by:
We're using the HttpRuntime Cache for our objects and we're trying to do the following. We pull lists of objects from the database and put them in cache. We put a reference to the list in...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
13
by: mliptak | last post by:
I'm trying to implement logging in my application, so that each log message has its unique identifier, e.g. log(identifier, text) What I want to achieve is that the compiler screams if the log()...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
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...
0
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
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...
1
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....
0
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
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
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...

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.