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

Dictionary.ContainsKey(object)

I'm using a generic Dictionary object where it is described as:

Dictionary<this, thatstuff = new Dictonary<this, that>()

Where THIS is a class with several property values.

This works fine as far as adding the key/value pairs to the dictionary.

The problem is when I contruct a new THIS object:

This this = new This();
this.a = "a";
this.b = "b";

and try to get a hit on:
if (stuff.ContainsKey(this))
{
...
}

Note: It works GREAT if I add this to the 'stuff' dictionary and then look
for the key, but not when I contruct a new key and try to match it to the
dictionary.

From research, I understand that it is because the dictionary object stores
a hash table value instead of the actual object as the key or something like
that and that is why they won't find it when I use Contains.

So, how do I work around this?
Sep 14 '07 #1
3 13651
Al Meadows wrote:
[...]
From research, I understand that it is because the dictionary object stores
a hash table value instead of the actual object as the key or something like
that and that is why they won't find it when I use Contains.

So, how do I work around this?
Your "This" class needs to implement a custom hash that depends on the
contents (ie override Object.GetHashCode()). By default, the reference
itself is used for hashing, and so two different instances result in two
different hash values.

By the way, for future reference, you may want to avoid using the word
"this" as your class name or variable name, given that it has very
specific, special meaning in C#. :)

Pete
Sep 14 '07 #2
Al Meadows wrote:
I'm using a generic Dictionary object where it is described as:

Dictionary<this, thatstuff = new Dictonary<this, that>()

Where THIS is a class with several property values.

This works fine as far as adding the key/value pairs to the dictionary.

The problem is when I contruct a new THIS object:

This this = new This();
this.a = "a";
this.b = "b";

and try to get a hit on:
if (stuff.ContainsKey(this))
{
...
}

Note: It works GREAT if I add this to the 'stuff' dictionary and then look
for the key, but not when I contruct a new key and try to match it to the
dictionary.

From research, I understand that it is because the dictionary object stores
a hash table value instead of the actual object as the key or something like
that and that is why they won't find it when I use Contains.

So, how do I work around this?

Hi,

you either have to override Equals and GetHashCode of your class or
implement a custom EqualityProvider for it:

class This
{
public string a, b;
public override bool Equals(object obj)
{
This rhs = obj as This;
if(rhs == null)
return false;
return Equals(a, rhs.a) && Equals(b, rhs.b);
}
public override int GetHashCode()
{
return a.GetHashCode() ^ b.GetHashCode();
}
}

class ThisEqualityComparer : IEqualityComparer<This>
{
public bool Equals(This x, This y)
{
return Equals(x.a, y.a) && Equals(x.b, y.b);
}

public int GetHashCode(This obj)
{
return obj.a.GetHashCode() ^ obj.b.GetHashCode();
}
}

IDictionary<This, Thatstuff =
new Dictionary<This, That>(new ThisEqualityComparer())
HTH,
Andy
--
You can email me by removing the NOSPAM parts below:
xm**********@gmxNOSPAM.net
Sep 14 '07 #3
Al Meadows wrote:
I'm using a generic Dictionary object where it is described as:

Dictionary<this, thatstuff = new Dictonary<this, that>()

Where THIS is a class with several property values.

This works fine as far as adding the key/value pairs to the dictionary.

The problem is when I contruct a new THIS object:

This this = new This();
this.a = "a";
this.b = "b";

and try to get a hit on:
if (stuff.ContainsKey(this))
{
...
}

Note: It works GREAT if I add this to the 'stuff' dictionary and then look
for the key, but not when I contruct a new key and try to match it to the
dictionary.

From research, I understand that it is because the dictionary object stores
a hash table value instead of the actual object as the key or something like
that and that is why they won't find it when I use Contains.

So, how do I work around this?

Hi,

you either have to override Equals and GetHashCode of your class or
implement a custom EqualityProvider for it:

class This
{
public string a, b;
public override bool Equals(object obj)
{
This rhs = obj as This;
if(rhs == null)
return false;
return Equals(a, rhs.a) && Equals(b, rhs.b);
}
public override int GetHashCode()
{
return a.GetHashCode() ^ b.GetHashCode();
}
}

class ThisEqualityComparer : IEqualityComparer<This>
{
public bool Equals(This x, This y)
{
return Equals(x.a, y.a) && Equals(x.b, y.b);
}

public int GetHashCode(This obj)
{
return obj.a.GetHashCode() ^ obj.b.GetHashCode();
}
}

IDictionary<This, Thatstuff =
new Dictionary<This, That>(new ThisEqualityComparer())
HTH,
Andy

--
You can email me by removing the NOSPAM parts below:
xm**********@gmxNOSPAM.net
Sep 14 '07 #4

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

Similar topics

18
by: Rune B | last post by:
Hi Group I was considering using a Generic Dictionary<> as a value container inside my business objects, for the reason of keeping track of fields changed or added and so on. - But how...
4
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
4
by: NullQwerty | last post by:
Hi folks, I have a Dictionary which contains a string key and an object value. I want the object value to point to a property in my class and I want it to be by reference, so that later on I...
70
by: jojoba | last post by:
Hello! Does anyone know how to find the name of a python data type. Conside a dictionary: Banana = {} Then, how do i ask python for a string representing the name of the above dictionary...
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):
6
by: Andrus | last post by:
I need to create object cache in memory. Each object id can be composed from several string, integer and decimal type values. ContainsKey does not find existing key in this case. How to...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
6
by: GiJeet | last post by:
hello, I'm trying to use a dictionary as a class member. I want to use a property to get/set the key/value of the dictionary but I'm confused as how to use a dictionary as a property. Since there...
3
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, I have a dictionary (C# 2.0) with an object (an instance of a class) for the key. The class has only a "name" field. dictionary<object, ...> When I use ContainsKey property of the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.