By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

Dictionary class lacks the very basic function ?

P: n/a
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 !?

Sep 22 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

craigkeniss...@hotmail.com wrote:
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.

Sep 22 '06 #2

P: n/a
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Sep 22 '06 #3

P: n/a
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 !

Sep 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.