• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

How To Modify An Element in the Dictionary Class?

complete

Weaksauce
Joined
Aug 30, 2005
Messages
92
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);
 
Code:
runningcount[city]++;

I don't do C# but this is pretty much what I was thinking, or if it's beyond a simple iteration

runningcount[city] += whatever

I believe i've done this in other languages.
 
"runningcount[city]" evaluates to a reference to the object you looked up with the key "city". With that reference, you can do anything you'd like to the object.
 
why don't you just write a test program to find out for yourself? What happens if you do:

FooDictionary[5] = new Bar();

when there is no element with index 5, and see what happens? Maybe it will throw an exception, maybe it won't.

You can also read the documentation and example program provided, it might tell you all the answers: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
 
As has been noted here, you can simply write to the dictionary by key.

myDictionary[myKey] = myValue

This will *add or replace* without giving you feedback on what has happened.

Consider whether you need to know when something has been inserted versus overwritten.
 
Back
Top