473,698 Members | 2,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6332
PS

"Danny Liberty" <dl******@gmail .comwrote in message
news:11******** **************@ e64g2000cwd.goo glegroups.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
1363
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 inputs) - body: i need to assign to the property readOnly the value true for all the objects in the array Anybody know how can i do this? Thanks,
20
4426
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
2706
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 examine the responsible Node's identifier to see which TreeNode triggered the event? Analyzing the Node's Text string is undesirable because it would mean performing a string compare to a set of predefined strings. This is slower and not easily...
1
4159
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 linked lists as follows: (a) A double linked list called NAMES which will contain all C like identifiers of less than 256 characters long identified in the input file F. Each identifier
5
2871
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 from a previous function. The pseudo code for this problem would be: returnedlist = count = 0 for i in returnedlist: if count < len(returnedlist):
6
1807
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 std::list to store this information by inserting objects containing all 3 pieces of information. However now some users has datasets in the range of 12000 records and my list seems to be taking like forever to complete inserting and finding.
0
5692
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 cache, but we also put references to the individual items so we can access them without having to search the lists. The objects and lists each have unique identifiers. The issue is when we want to update the object. Lets say we pull an individual...
23
5727
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 or Array, correct? An example: create the object, create an array, the stuff the object into the array. Later on, assume the object is mutable, the object changes, but you can find it, if you have enough state information to uniquely identify...
13
3705
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() with 'identifier' is also used in some other place in the code which would make the 'identifier' not unique. Is that something that can be achieved in C++? Thanks
0
8683
marktang
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...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
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...
1
8904
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,...
0
7741
agi2029
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...
0
5867
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();...
0
4372
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...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.