Solved

.NET Collections - Changing the Key identifier (NameObjectCollectionBase)

Posted on 2003-11-18
10
287 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

867 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

22 Experts available now in Live!

Get 1:1 Help Now