Dictionary class lacks the very basic function ?

  • Thread starter Thread starter craigkenisston
  • Start date Start date
C

craigkenisston

I want to use Dictionary class like this:

textboxM.Text = oMyDict.GetValue("textboxM");
textboxN.Text = oMyDict.GetValue("textboxN");

Where my dictionary contains the list of values for my textbox based on
the textbox id.

But it doesn't have it !

TryGetValue !? What's that ? Why I have to provide an out parameter,
isn't more simple just return the value or null if not found !?
 
TryGetValue !? What's that ? Why I have to provide an out parameter,
isn't more simple just return the value or null if not found !?

What if "null" is actually the value corresponding to that key?
Example:

Dictionary Dict = new Dictionary();
Dict.Add("key", null);

Now, using your preferred method, the call would be something like:

object value = Dict["key"];

How do you know if it was there or not? In this situation (i.e. the
function -returns- the value), the only way to know is to have the
function (the indexer) throw an exception if it's not found. And in
fact, Dictionary does throw an exception if an entry with the key you
specified is not found. TryGetValue is useful though in a situation
where there is a chance the key might not be there.

object value;
bool Result = Dict.TryGetValue("key", out value);
if (!Result)
{
//The key wasn't in the dictionary.
}


In the first situation, where you use the indexer, it's still possible
to guarantee no exception will be thrown if you use code like this:

if (Dict.ContainsKey("key"))
object value = Dict["key"];

But performance suffers now, because you are doing -two- separate
lookups into the hash table. One for ContainsKey, then doing the exact
same lookup again to fetch it.

I admit, using out parameters feels clunky sometimes and inelegant, but
in this case it is the most elegant and performance efficient way to
handle everything with one function call.
 
I want to use Dictionary class like this:
textboxM.Text = oMyDict.GetValue("textboxM");
textboxN.Text = oMyDict.GetValue("textboxN");

Where my dictionary contains the list of values for my textbox based on
the textbox id.

But it doesn't have it !

Use the indexer

textboxM.Text = oMyDict["textboxM"];

TryGetValue !? What's that ? Why I have to provide an out parameter,
isn't more simple just return the value or null if not found !?

A Dictionary can be instantiated with a value type that isn't
nullable, so returning null isn't always an option. That's why
TryGetValue is needed.


Mattias
 
I ended up writing a variation of Dictionary class:

public class StringDictionary : Dictionary<string, string>
{

public string GetValue(string key)
{
string outvalue;

if (this.TryGetValue(key, out outvalue) == false)
{
outvalue = "";
};

return outvalue;
}

}

So, simple !

Thanks you all !
 

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

Back
Top