CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2004
    Posts
    265

    Rename dictionary key

    Hi,

    Is it possible to change the key of a dictionary?

    Thanks

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Rename dictionary key

    What Dictionary ??? ... What Key ???


    Encryption ??? What Language ????

    More info needed to process....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Sep 2011
    Location
    The Netherlands
    Posts
    32

    Re: Rename dictionary key

    I assume he is talking about the default Dictionary class:
    http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

  4. #4
    Join Date
    Apr 2004
    Posts
    265

    Re: Rename dictionary key

    Sorry, it's the dictionary class that contains keys and values. I have a dictionary that contains a key. I anyway retrieved values of one particular key's values, added two new keys, and added the same values to the newley added keys. But when I update the value of one key, it updates the value of the other key as well. Any idea why?

    Thanks

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Rename dictionary key

    Post the code that your using, so that we can see how and why...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Oct 2011
    Posts
    25

    Re: Rename dictionary key

    No, you can't rename the key of a dictionary entry directly. However, you can copy the value from the old entry to a new entry and then delete the old one. Something like this:
    Code:
    myDictionary.Add(preferredKeyName, myDictionary(oldKeyName))
    myDictionary.Remove(oldKeyName)

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Rename dictionary key

    Never ever do that. Sorry, but my opinion is why create the dictionary in the first place then. I always have this discussion with my students. My answer is always the same. Make sure you only use what is needed. If you have to half way through your program EDIT the dictionaries, your planning is wrong. Yes, maybe there is a need for this, but in my opinion it is just edit programming ( where you keep on editing things just to keep your app going ), instead of of proper programming ( where you almost never need to edit existing objects - and your app is behaving good )

    Just saying....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured