From: cate on
On May 9, 11:23 am, Family Tree Mike <FamilyTreeM...(a)ThisOldHouse.com>
wrote:
> 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.
>
> --
> Mike

I want the last.
From: Peter Duniho on
cate wrote:
> On May 9, 11:23 am, Family Tree Mike <FamilyTreeM...(a)ThisOldHouse.com>
> wrote:
>> 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
From: cate on
On May 9, 11:38 am, Peter Duniho <no.peted.s...(a)no.nwlink.spam.com>
wrote:
> cate wrote:
> > On May 9, 11:23 am, Family Tree Mike <FamilyTreeM...(a)ThisOldHouse.com>
> > wrote:
> >> 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);
}