Solved

.NET Collections - Changing the Key identifier (NameObjectCollectionBase)

Posted on 2003-11-18
10
286 Views
Last Modified: 2007-12-19
Hi. I'm grappling with .NET Collections - great prospects but a milllion miles from VB6 Collection Object (why did they not include a similar abstract class to this??). Anyway, I've decided to use the NameObjectCollectionBase and build on that to create a strong typed Collection Class. It will be eaiser to explain my problem through an example i guess:

Suppose I have an Person Class (Two Properties: $FirstName and $SecondName) and the collection class is Persons and implements the usual Add, Item, Remove etc...The Key by which individuals are referenced is the concatanation of the First & Second names (i.e. MichaelJackson)

So,

Dim People As New Persons

People.Add(New Person(David, Beckham)) 'Person Constructor allows both names as arguments...
People.Add(New Person(Victoria, Adams)) 'I do apologise...

So two members are added to the collection and can be referenced through the Item property via the keys (DavidBeckham and VictoriaAdams). Now imagine these two people get married, and, deciding to take her partners name, Victoria Adams becomes Victoria Beckham.

Changing her surname is no prob...

People.Item("VictoriaAdams").SecondName = "Beckham"

How though can I make changes on the Key which enables access through Item as the collection is niether aware or cares about the wedding and still thinks she's Miss Adams?

There used to be a trick to do this in VB6, its here if anyones interested http://archive.devx.com/free/tips/tipview.asp?content_id=3772

but is it possible in the .NET NameObjectCollectionBase where the Keys Collection is ReadOnly. Is this the right way to go about this type of problem at all?
0
Comment
Question by:David_Stone
  • 4
  • 3
  • 3
10 Comments
 

Author Comment

by:David_Stone
ID: 9772797
Thanks!! Any help very much appreciated.....
0
 
LVL 42

Accepted Solution

by:
frodoman earned 125 total points
ID: 9772950
What I've always done in this situation is to implement a "NameChange" method.  

This method accepts a from & to parameter (or 2 from & 2 parameters which can be concatenated to produce a key) and creates a new Person (using the 'to' parameter).  Then it copies all properties from the Person object keyed by the 'from' parameter, and then finally destroys the old Person.

I wouldn't swear this is the best method, but it works and it's easy to understand years later when you have to go back to the code again...
0
 
LVL 15

Expert Comment

by:SRigney
ID: 9774147
I agree with frodoman.  Implementing some kind of change function that removes the item and adds it back with the new key would be best.  That or changing the key to something that doesn't change.  Even though Ms Adams becomes Mrs. Becham her SSN doesn't change.
0
 
LVL 42

Expert Comment

by:frodoman
ID: 9774281
SRigney,

You make a valid point, but only if you have access to SSN or something similar.  These days, it's getting rare to have access to this even when dealing only with internal employees.  Some legislation floating around Washington may make it even worse on us (developers) so personally I'd stay away from this option...

Frodoman

0
 

Author Comment

by:David_Stone
ID: 9774517
Thanks for your help ppl...It's a shame it can't be done, it'd make renaming objects in a tree view, list, whatever a much easier task. I think i'll adopt another approach, thinking out aloud here - is it common to associate a unique random number for each object which, as commented by SRigney, will never change? Whats SSN btw?

Cheers....
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:SRigney
ID: 9774553
SSN = Social Security Number.  It's an ID given by the government to practically everyone. You have to have one for taxes and retirment benefits.

Many times it is common to create a unique random number for each object.  The purpose of using a key is that the key itself is totally unique.  The situation you give does not guarantee uniqueness.  JohnSmith might be valid for many different people.  .NET provides the NewID() function that creates a GUID.  GUIDs are completely unique.  

You can probably use either a GUID or just an incremental counter for your app depending on how you access the record to start with.
0
 

Author Comment

by:David_Stone
ID: 9774559
Social Security Number per chance!?
0
 
LVL 15

Expert Comment

by:SRigney
ID: 9774561
By the way.  SSN is an American thing.  It doesn't work for Canada.  And I have no idea what would be any kind of ID in England.
0
 
LVL 42

Expert Comment

by:frodoman
ID: 9774587
"SSN" is social security number.  That's not to say it can't be done - just that you may have a hard time getting people to give you their social #.  Also run into issues with non-US residents - if that's a consideration for your application.

It is **VERY** common to use a unique number as the key for objects like this.  If you want to use a random identifier, then look into using a GUID which isn't really random, but appears to be for most purposes.  If it isn't important to be random, it's common to simply use a number that increments by 1 each time - so the first person is keyed on number 00001 regardless of their name, next is 00002, etc.

Hope that helps!
0
 

Author Comment

by:David_Stone
ID: 9774851
It's the National Insurance Number over here which is your main unique idenifier but there is a socail security number too - not too sure what it is though - maybe only applicable if you draw benefit etc. The example i gave was completely abstract you understand - my app is nothing to do with ppl - it's a db app but an interesting conv all the same! thanks for help-i'll use the NewID function....cheers....:)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DataGridView 2 29
Linux operating system 12 66
DataGridview Currency Formating? 22 39
Code works but how can I download the file? 20 47
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now