473,416 Members | 1,729 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,416 software developers and data experts.

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.Collections.Generic.Comparer.Default checks whether the key type K
implements Generic System.IComparable and uses that implementation, if
available. If not, Generic System.Collections.Generic.Comparer.Default
checks whether the key type K implements System.IComparable. If the key type
K does not implement either interface, you can specify a Generic
System.Collections.Generic.IComparer implementation in a constructor
overload that accepts a comparer parameter."

When I query executeTable.Comparer, it returns this:
{System.Collections.Generic.ObjectEqualityComparer <Test.OperationEntityType>
}

[System.Collections.Generic.ObjectEqualityComparer< Test.OperationEntityType>
]:
{System.Collections.Generic.ObjectEqualityComparer <Test.OperationEntityType>
}
FYI, I'm using VS 2005 Beta 2.

Thanks,
Erik

_________________________________

using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
public class OperationEntityType : IComparable<OperationEntityType>
{
public int m_operationId;
public Type m_entityType;
public OperationEntityType(int aOperationId, Type aEntityType)
{
m_operationId = aOperationId;
m_entityType = aEntityType;
}
public int CompareTo(OperationEntityType other)
{
if ((other.m_operationId == m_operationId)
&& (other.m_entityType == m_entityType))
return 0;
else
return 1;
}

public bool Equals(OperationEntityType other)
{
return (CompareTo(other) == 0);
}

}
public delegate object OperationExecuteDelegate(object aContext);

public class Class1
{
public static object OnExecute(object aContext)
{
Dictionary<OperationEntityType,
OperationExecuteDelegate> executeTable =
new Dictionary<OperationEntityType,
OperationExecuteDelegate>();

OperationEntityType m_firstOperation = new
OperationEntityType(1,
typeof(string));

executeTable.Add(m_firstOperation,
null);

OperationExecuteDelegate execute;

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

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

return execute(aContext);
}

public static void Main()
{
OnExecute(null);
}
}
}
Nov 17 '05 #1
2 15859
On Thu, 16 Jun 2005 12:27:15 -0500, "ESPNSTI"
<ES*********@Hotmail.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 IEqualityComparer interface to locate
keys, not IComparable.CompareTo.

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

If you need to use IComparable you should try SortedDictionary 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.com...
On Thu, 16 Jun 2005 12:27:15 -0500, "ESPNSTI"
<ES*********@Hotmail.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 IEqualityComparer interface to locate
keys, not IComparable.CompareTo.

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

If you need to use IComparable you should try SortedDictionary 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
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,...
1
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...
7
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...
4
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
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...
8
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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,...
0
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...

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.