?
Solved

.NET Collections - Changing the Key identifier (NameObjectCollectionBase)

Posted on 2003-11-18
10
Medium Priority
?
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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