Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

.NET Collections - Changing the Key identifier (NameObjectCollectionBase)

Posted on 2003-11-18
10
Medium Priority
?
298 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

636 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