Dictionary.Add() defeat already exists exception

  • Thread starter Thread starter cate
  • Start date Start date
C

cate

Is there a switch somewhere, or a different hash, that will allow me
to add the same key again and again with out throwing?
Thank you.
 
Is there a switch somewhere, or a different hash, that will allow me
to add the same key again and again with out throwing?
Thank you.

If you had two keys named "FindMe", one pointing to "Fred", one pointing
to "Barney", then how would code know which to return for MyDictionary
["FindMe"]?

There actually is a trick, but to use the trick, you would need to have
a good explanation as to why you want duplicate strings as keys.
 
Is there a switch somewhere, or a different hash, that will allow me
to add the same key again and again with out throwing?
Thank you.

If you had two keys named "FindMe", one pointing to "Fred", one pointing
to "Barney", then how would code know which to return for MyDictionary
["FindMe"]?

There actually is a trick, but to use the trick, you would need to have
a good explanation as to why you want duplicate strings as keys.

I want the last.
 
cate said:
Is there a switch somewhere, or a different hash, that will allow me
to add the same key again and again with out throwing?
Thank you.
If you had two keys named "FindMe", one pointing to "Fred", one pointing
to "Barney", then how would code know which to return for MyDictionary
["FindMe"]?

There actually is a trick, but to use the trick, you would need to have
a good explanation as to why you want duplicate strings as keys.

I want the last.

What does that mean precisely?

If you simply want to replace the current value for a given key with a
new value, then you either have to call the Remove() method to remove
the current value before calling Add() to add the new value, or you can
just use the indexer property and assign the new value directly (e.g.
"myDictionary[keyStringVariable] = valueObjectVariable;")

If you want a dictionary to store more than one object for a given key,
you will have to store as the value for the key not the actual object,
but instead a list of objects.

If you are willing and able to change the definition of "key" so that it
includes more than just the string itself, then there are a variety of
ways to encapsulate that "string plus something" key, such that each
object in the dictionary can still be uniquely identified in spite of
having the same string associated with it.

The phrase "I want the last" is too vague for me to know for sure what
you mean, but hopefully one of the above ideas will apply to your
specific scenario.

Pete
 
cate said:
On 5/9/2010 12:16 PM, cate wrote:
Is there a switch somewhere, or a different hash, that will allow me
to add the same key again and again with out throwing?
Thank you.
If you had two keys named "FindMe", one pointing to "Fred", one pointing
to "Barney", then how would code know which to return for MyDictionary
["FindMe"]?
There actually is a trick, but to use the trick, you would need to have
a good explanation as to why you want duplicate strings as keys.
I want the last.

What does that mean precisely?

If you simply want to replace the current value for a given key with a
new value, then you either have to call the Remove() method to remove
the current value before calling Add() to add the new value, or you can
just use the indexer property and assign the new value directly (e.g.
"myDictionary[keyStringVariable] = valueObjectVariable;")

If you want a dictionary to store more than one object for a given key,
you will have to store as the value for the key not the actual object,
but instead a list of objects.

If you are willing and able to change the definition of "key" so that it
includes more than just the string itself, then there are a variety of
ways to encapsulate that "string plus something" key, such that each
object in the dictionary can still be uniquely identified in spite of
having the same string associated with it.

The phrase "I want the last" is too vague for me to know for sure what
you mean, but hopefully one of the above ideas will apply to your
specific scenario.

Pete

Thanks Pete. I went the remove route

private static void addOp(string key, string value) {
if (ops.ContainsKey(key)) {
ops.Remove(key);
}
ops.Add(key, value);
}
 
Is there a switch somewhere, or a different hash, that will allow me
to add the same key again and again with out throwing?

The term Add indicates that a new entry is added. If a new entry
is not added, then it seems rather fair that an exception is
thrown.

The syntax:

dic[key] = val;

allows you to both add and update with the same syntax.

Note that this syntax does not in current implementation
do a test and a remove if already exists - both Add and
this set calls the same private Insert method which determines
whether to throw an exception or update the value based on a flag
passed on to it.

Arne
 
Back
Top