Jump to content

How To Modify An Element in the Dictionary Class?


Xarzu

Recommended Posts

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?

 

int value = runningcount[city];

runningcount.Remove(city);

runningcount.Add(city, ++value);

Link to comment
Share on other sites

I have no idea why you're using that horrible proprietary rip-off of Java (which I also don't have too many kind words for) when even Microsoft themselves have ditched it in favour of a return to C++, but that's just me being a language snob. But I would recommend you focus on learning Python and C++ instead. Between those two languages you've got a super powerful toolset for tackling pretty much any problem, and as an added benefit, you can embed them in each other so you can use both at once to solve a single problem. Obviously Python's strength is scripting and rapid prototyping, while C++'s strength is power, ubiquity, and performance.

 

Anyway the answer is yes.

 

runningcount[city] = runningcount[city] + 1;

 

BTW: the Dictionary class isn't like a hash table, it is a hash table.

Edited by Krezack
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...