Why does it behave differently

T

Tony Johansson

Hi!

If you for example have a StringDictionary and you add by using key as index
like this
stringdictionaryInstance["myKey"] = "myValue1"
stringdictionaryInstance["myKey"] = "myValue2"
and have identical keys as I have in this example only the last one is added
which is ok.

If I now add by using the add method instead as I do here
stringdictionaryInstance.Add("myKey", "myValue1")
stringdictionaryInstance.Add("myKey", "myValue2")
I get exeption because of identical keys.

So my first question is why does it behave differently ?
I mean in the last example when I used the add only the last one would have
been added is it is with the first example

It's also behave differently when I use the NameValueCollection beween
adding using
the add method and when I add in this way
NameValueCollectioninstanc["Key"] = "Some Text"
NameValueCollectioninstanc["Key"] = "Some more Text"

It behave as it it documented but I just wonder why behave differently ?

//Tony


//Tony
 
W

Willem van Rumpt

Tony said:
Hi!

So my first question is why does it behave differently ?
I mean in the last example when I used the add only the last one would have
been added is it is with the first example

I would say because of the implicit contract that comes with a method
called "Add". A method called "Add" clearly implies that it will add
something (to a dictionary in this case). If it were to either add, or
update something, the name "Add" would've been a very poor choice for
that functionality.
 
F

Family Tree Mike

Hi!

If you for example have a StringDictionary and you add by using key as index
like this
stringdictionaryInstance["myKey"] = "myValue1"
stringdictionaryInstance["myKey"] = "myValue2"
and have identical keys as I have in this example only the last one is added
which is ok.

If I now add by using the add method instead as I do here
stringdictionaryInstance.Add("myKey", "myValue1")
stringdictionaryInstance.Add("myKey", "myValue2")
I get exeption because of identical keys.

So my first question is why does it behave differently ?
I mean in the last example when I used the add only the last one would have
been added is it is with the first example

It's also behave differently when I use the NameValueCollection beween
adding using
the add method and when I add in this way
NameValueCollectioninstanc["Key"] = "Some Text"
NameValueCollectioninstanc["Key"] = "Some more Text"

It behave as it it documented but I just wonder why behave differently ?

//Tony


//Tony

Because your second call is not doing an add, but rather an update. So
the two sections of code are not equivalent.
 
P

Peter Duniho

Tony said:
Hi!

If you for example have a StringDictionary and you add by using key as index
like this
stringdictionaryInstance["myKey"] = "myValue1"
stringdictionaryInstance["myKey"] = "myValue2"
and have identical keys as I have in this example only the last one is added
which is ok.

If I now add by using the add method instead as I do here
stringdictionaryInstance.Add("myKey", "myValue1")
stringdictionaryInstance.Add("myKey", "myValue2")
I get exeption because of identical keys.

[...]
It behave as it it documented but I just wonder why behave differently ?

The short answer is: because that's how the class is designed.

More generally, you are incorrect to claim that your first code example
is _adding_ values. You are using the object indexer to _set_ a value.
It has the added convenience that if the value doesn't yet exist, it
will be added on your behalf. But semantically, the operation isn't an
"add".

On the other hand, in the second example, semantically the operation
clearly is an "add". That's the name of the method. And of course, an
"add" implies that there will be an _additional_ value, which is
impossible if the value already exists.

Thus the exception when you are _adding_, but no exception when you are
_setting_.

Pete
 
M

mungflesh

the operator [] is an indexed property of type TValue, which we assume
from your example as being a string and whose index type is TKey,
which is also a string.

this property has a get & set.

1a. for the get, which is not highlighted in your example,

string value = stringdictionaryInstance["myKey"];

would throw an exception if the key myKey does not exist. the method
Add(key,value) needs to be called for this key first or the set part
of the property

1b. for the set portion, which you have highlighted, the key myKey
will be checked for existence and added if not there, such that the
value myValue1 can be assigned to it in the same property call. the
second line of code repeats the process but since the key now exists,
it just re-assigns the existing entry to the new value myValue2.

I tend to avoid usage of the "get" portion of operator[], in favour of
the more explicit bool TryGetValue(key, out value) method, which
complements the Add method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top