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 1 6332
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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,
|
by: William Stacey [MVP] |
last post by:
int list = {1,2,3,4,5,6};
Function to randomize the list?
Cheers!
--
William Stacey, MVP
|
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...
|
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
|
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):
| |
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.
|
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...
|
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...
|
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
|
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: 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...
| |
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: 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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |