Bookmark Collection: Retrieving a bookmark that is not in the collection
Let's see where we were...
- Retrieve a Bookmark that is not in the collection, return null
The test for this looks like this:
[
Test]
public void RetrieveABookmarkNotInCollection()
{
Assert.IsNull(collection[exampleDotComLabel]);
}
When I run this I get the following error:
BookmarkCollectionFixture.RetrieveABookmarkNotInCollection : System.Collections.Generic.KeyNotFoundException : The given key was not present in the dictionary.
Unfortunately it does not return null like I want it to. I guess I will have to write some code. I need to modify the Indexer to check to see if the label is in the collection. If not it should return null. Here's the code:
public Uri this[string label]
{
get
{
if (!dictionary.ContainsKey(label)) return null;
return dictionary[label];
}
}
I compile and run and the tests pass. Let's move on. The next post will address the following tests:
- Add a Bookmark with a null label, expect ArgumentNullException
- Add a Bookmark with a null URL, expect ArgumentNullException
Surveying the rest of the tests in the test list indicates that there is not much left so I should be able to finish up the task in the next couple of posts.
Comments
- Anonymous
December 27, 2004
This an extremely off-topic comment, but I have never understood why the library was written this way. In this code
if (!dictionary.ContainsKey(label))
return null;
return dictionary[label];
The lookup is actually performed twice for objects in the dictionary. What a horrible performance penalty! The STL and MFC approaches seems much more sane in that regard. - Anonymous
December 27, 2004
Interesting read ;), I just came through.
My question is, how many of us would rather write:
if (!dictionary.ContainsKey(label))
return null;
else
return dictionary[label];
--
It makes no real difference, but for me, it feels more 'right'.
I want to return null if dictionary doesn't contain the key, else I want to return the value.
Anybody else? :)
RGab