#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    227
    Rep Power
    0

    How To Modify An Element in the Dictionary Class?


    How To Modify An Element in the Dictionary Class?
    ===============================

    C# has this cool Dictionary class that you can use like a Hash Table. Is there a way of changing the value of an indexed element without resorting to removing it like this?
    Code:
                   int value = runningcount[city];
                   runningcount.Remove(city);
                   runningcount.Add(city, ++value);
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,749
    Rep Power
    9397
    You probably can't ++ it but you can
    Code:
    runningcount[city] = runningcount[city] + 1;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    140
    Rep Power
    0

    Answer of Your problem


    we could assign the value of an indexed element of the dictionary without removing it as follows:

    Dictionary<string, int> runningcount = new Dictionary<string, int>();

    runningcount.Add("city", 123);
    runningcount["city"] = ++runningcount["city"];

IMN logo majestic logo threadwatch logo seochat tools logo