472,958 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 15703
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 ??
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.