• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

.NET Collections - Changing the Key identifier (NameObjectCollectionBase)

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
David_Stone
Asked:
David_Stone
  • 4
  • 3
  • 3
1 Solution
 
David_StoneAuthor Commented:
Thanks!! Any help very much appreciated.....
0
 
frodomanCommented:
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
 
SRigneyCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
frodomanCommented:
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
 
David_StoneAuthor Commented:
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
 
SRigneyCommented:
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
 
David_StoneAuthor Commented:
Social Security Number per chance!?
0
 
SRigneyCommented:
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
 
frodomanCommented:
"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
 
David_StoneAuthor Commented:
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now