473,761 Members | 9,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How Dictionary<TKey ,TValue> is checking keys?

I have Dictionary that has custom class as its key. For that class I
implemented both Equals(object) and IComparable<T>. I keep getting
KeyNotFoudExcep tion.
What should I do?
Thanks
Shimon
Jun 13 '06 #1
2 1758
You should implement GetHashCode() by overriding this method from
System.Object in your class used as key.

"Shimon Sim" <sh**********@c ommunity.nospam > wrote in message
news:uH******** ******@TK2MSFTN GP04.phx.gbl...
I have Dictionary that has custom class as its key. For that class I
implemented both Equals(object) and IComparable<T>. I keep getting
KeyNotFoudExce ption.
What should I do?
Thanks
Shimon

Jun 13 '06 #2
Thanks it worked.
Shimon
"Lebesgue" <le******@gmail .com> wrote in message
news:uz******** ******@TK2MSFTN GP02.phx.gbl...
You should implement GetHashCode() by overriding this method from
System.Object in your class used as key.

"Shimon Sim" <sh**********@c ommunity.nospam > wrote in message
news:uH******** ******@TK2MSFTN GP04.phx.gbl...
I have Dictionary that has custom class as its key. For that class I
implemented both Equals(object) and IComparable<T>. I keep getting
KeyNotFoudExc eption.
What should I do?
Thanks
Shimon


Jun 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
21411
by: Brett Romero | last post by:
If I want to take action on the Add event of a generic Dictionary, do I need to create a custom Dictionary and add an event handler for the Add() method? The dictionary is a public field on a custom control. Thanks, Brett
7
25700
by: Russell Hind | last post by:
I want to create an array of key value pairs. This code compiles fine: KeyValuePair<string, object> kvps = { new KeyValuePair<string,object>( "int", 5 ), new KeyValuePair<string,object>( "string", "robot") }; But is their no shorthand to this such as KeyValuePair<string,object> kvps = {{"int",5 },{"string","a string"}};
6
46187
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: <BEGIN CODE> List<stringkeyNameList = new List<string>(); foreach (string keyName in this.myDictionary.Keys)
7
6140
by: bonk | last post by:
Hello I am acessing a Dictionary<TKey,TValuefrom multiple threads and often in a foreach loop. While I am within one of the foreach loops the other threads must not modify the collection itself since that would cause an exception in the foreach loop "foreach can not continue because the colelction was modifed". Now what is the least expensive and threadsafe way to make sure that no other threads modifies that collection. Since one of the...
10
3789
by: Chris Mullins [MVP] | last post by:
KeyedCollection is a very handy little class, that unforutnatly has a nasty bug in it. The bug (which I ran across) causes the following code to fail: if (!rooms.Contains(room)) _rooms.Add(room); The problem is that contains returns "false", but then Add throws an exception because the item really is already in there.
44
39180
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume List<is far better. So, why do people use ArrayList so often? Am I missing somehing? What's the difference between them? Zytan
4
10682
by: Peter K | last post by:
Hi are there any benefits in using StringDictionary over Dictionary<string, string? It appears they achieve the same thing... (I could be wrong of course). thanks, Peter
6
1814
by: daohuy.hua | last post by:
The context is that I have a C# class named MainModel which has a private Dictionary<string, FileStreammember named dict. I also have a property Dict to access to this member: public Dictionary<string, FileStreamDict { get { return this.dict; } } The problem with this implementation is that I only want to give
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9957
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.