473,394 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Dictionary class lacks the very basic function ?

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
3 1589

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
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tobias Pfeiffer | last post by:
Hi! Damnit, am I angry! Please look at the following example: class bla: def __init__(self): self.ho = {'a': , 'c': , 'b': , 'e': , 'd': , 'g': , 'f': , 'h': } # shall be an adjacence list...
4
by: brianobush | last post by:
# # My problem is that I want to create a # class, but the variables aren't known # all at once. So, I use a dictionary to # store the values in temporarily. # Then when I have a complete set, I...
125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
1
by: Martin Widmer | last post by:
Hi Folks. When I iterate through my custom designed collection, I always get the error: "Unable to cast object of type 'System.Collections.DictionaryEntry' to type...
7
by: noro | last post by:
Is it possible to do the following: for a certain class: ---------------------------- class C: def func1(self): pass def func2(self):
8
by: akameswaran | last post by:
I wrote up a quick little set of tests, I was acutally comparing ways of doing "case" behavior just to get some performance information. Now two of my test cases had almost identical results which...
20
by: Gustaf | last post by:
This is two questions in one really. First, I wonder how to convert the values in a Dictionary to an array. Here's the dictionary: private Dictionary<Uri, Schemaschemas = new Dictionary<Uri,...
0
by: Calvin Spealman | last post by:
On Thu, Jul 17, 2008 at 7:45 AM, mk <mrkafk@gmail.comwrote: As was pointed out already, this is a basic misunderstanding of assignment, which is common with people learning Python. To your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.