473,842 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dictionary<> not using IComparable<> to find keys?

Hi,

I'm trying to use a generics dictionary with a key class that implements and
needs IComparable<>.
However when I attempt to use the dictionary, it doesn't appear to use the
IComparable<> to find the key.
In the example below, accessing the dictionary by using the exact key object
that was used to add to the dictionary works.
(see code comment 1).
However, if I attempt to access the dictionary by using a key object that
was newly created, but one that does satisfy the IComparable<> condition,
then I get a "The given key was not present in the dictionary." exception.
(see code comment 2).

Am I reading this wrong, or should this really work?
Is there something that I'm doing wrong?

According to help:
"Generic Dictionary requires a comparer implementation to perform
comparisons. The default comparer Generic
System.Collecti ons.Generic.Com parer.Default checks whether the key type K
implements Generic System.ICompara ble and uses that implementation, if
available. If not, Generic System.Collecti ons.Generic.Com parer.Default
checks whether the key type K implements System.ICompara ble. If the key type
K does not implement either interface, you can specify a Generic
System.Collecti ons.Generic.ICo mparer implementation in a constructor
overload that accepts a comparer parameter."

When I query executeTable.Co mparer, it returns this:
{System.Collect ions.Generic.Ob jectEqualityCom parer<Test.Oper ationEntityType >
}

[System.Collecti ons.Generic.Obj ectEqualityComp arer<Test.Opera tionEntityType>
]:
{System.Collect ions.Generic.Ob jectEqualityCom parer<Test.Oper ationEntityType >
}
FYI, I'm using VS 2005 Beta 2.

Thanks,
Erik

_______________ _______________ ___

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace Test
{
public class OperationEntity Type : IComparable<Ope rationEntityTyp e>
{
public int m_operationId;
public Type m_entityType;
public OperationEntity Type(int aOperationId, Type aEntityType)
{
m_operationId = aOperationId;
m_entityType = aEntityType;
}
public int CompareTo(Opera tionEntityType other)
{
if ((other.m_opera tionId == m_operationId)
&& (other.m_entity Type == m_entityType))
return 0;
else
return 1;
}

public bool Equals(Operatio nEntityType other)
{
return (CompareTo(othe r) == 0);
}

}
public delegate object OperationExecut eDelegate(objec t aContext);

public class Class1
{
public static object OnExecute(objec t aContext)
{
Dictionary<Oper ationEntityType ,
OperationExecut eDelegate> executeTable =
new Dictionary<Oper ationEntityType ,
OperationExecut eDelegate>();

OperationEntity Type m_firstOperatio n = new
OperationEntity Type(1,
typeof(string)) ;

executeTable.Ad d(m_firstOperat ion,
null);

OperationExecut eDelegate execute;

// 1) This works.
execute = executeTable[m_firstOperatio n];

// 2) This produces a "The given key was not present in the dictionary."
exception.
execute = executeTable[new OperationEntity Type(1,
typeof(string))];

return execute(aContex t);
}

public static void Main()
{
OnExecute(null) ;
}
}
}
Nov 17 '05 #1
2 15957
On Thu, 16 Jun 2005 12:27:15 -0500, "ESPNSTI"
<ES*********@Ho tmail.com> wrote:
I'm trying to use a generics dictionary with a key class that implements and
needs IComparable<>.
However when I attempt to use the dictionary, it doesn't appear to use the
IComparable< > to find the key.


Certainly not. Dictionary<> is a hashtable which means it's using
GetHashCode and Equals from the IEqualityCompar er interface to locate
keys, not IComparable.Com pareTo.

The documentation you quote is simply wrong. Apparently someone was
confusing IComparer with IEqualityCompar er. The most recent June CTP
release of Visual Studio 2005 has the correct documentation.

If you need to use IComparable you should try SortedDictionar y or
SortedList instead. These collections use IComparable to find a key.
--
http://www.kynosarges.de
Nov 17 '05 #2
Thanks!

"Christoph Nahr" <ch************ @kynosarges.de> wrote in message
news:8s******** *************** *********@4ax.c om...
On Thu, 16 Jun 2005 12:27:15 -0500, "ESPNSTI"
<ES*********@Ho tmail.com> wrote:
I'm trying to use a generics dictionary with a key class that implements andneeds IComparable<>.
However when I attempt to use the dictionary, it doesn't appear to use theIComparable< > to find the key.


Certainly not. Dictionary<> is a hashtable which means it's using
GetHashCode and Equals from the IEqualityCompar er interface to locate
keys, not IComparable.Com pareTo.

The documentation you quote is simply wrong. Apparently someone was
confusing IComparer with IEqualityCompar er. The most recent June CTP
release of Visual Studio 2005 has the correct documentation.

If you need to use IComparable you should try SortedDictionar y or
SortedList instead. These collections use IComparable to find a key.
--
http://www.kynosarges.de

Nov 17 '05 #3

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

Similar topics

8
17853
by: Brian P | last post by:
I want to expose a property of Dictionary<string, MyAbstractClass>. I tried to do it this way: private Dictionary<string, MyChildClass> _dictionary; public Dictionary<string, MyAbstractClass> { get { return _dictionary; } //error: no implicit conversion. }
1
2547
by: Eran | last post by:
Hi, I have a huge data structure, which I previosly stored in a Dictionary<int, MyObj> MyObj is relatively small (2 int, 1 DateTime, 1 bool). The dictionary I am using is quite large (25,000), and I have 500 such dictionaries. What I've noticed is that the total memory consumed became over 1 GB. When I changed the implementation to List<MyObj>, or SortedList<int,
7
57568
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
4
10689
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
4
4872
by: Mark S. | last post by:
Hello, I have a series of changing string IDs that are loaded dynamically a couple times a minute. I need to associate each ID with a different static class so later on in the app's lifecycle it knows which static class to use for processing. I've tried: static class MyStaticObject {
8
7120
by: Peter Larsen [CPH] | last post by:
Hi, How do i concat two dictionaries aka the following sample: Dictionary<int, stringa; Dictionary<int, stringb; b.Add(a); What is the easiest way to concat two dictionaries ??
0
9871
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
9715
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10944
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10610
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
10672
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
10313
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...
1
7855
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5696
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...
1
4499
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.