Wrox Programmer Forums
|
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 21st, 2005, 11:26 AM
Registered User
 
Join Date: Nov 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with generics collections

Problem with Generic.Dictionary and Generic.SortedDictionary.
ContainsValue function of both of these classes work with usuall datatypes. But when you make a collection of the objects of a class that you have defined yourself it doesnt work.

I know that it requires you to implement IComparable which i have done. And this implementation works fine for the other function - Sort- that demands.

Please help.

Apologies for posting this query more than once. But i'm just making sure that it gets attention of people in all the relevant forums.

 
Old November 21st, 2005, 01:41 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

I think you have to implement the IEquatable interface to define the Equals method for the class in question. IComparable is for ordering instances of a class (e.g. for sorting), but I'm pretty sure ContainsValue doesn't use IComparable, but instead IEquatable.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old November 21st, 2005, 05:28 PM
Registered User
 
Join Date: Nov 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perfact. Jeff, u saved some of my hair from being uprooted. This forum lived up to its reputation. It's my first expereriance and it has been a life saver. Thanks.
But now I'm wondering why is the documentation misleading people. It hardly said anything about this interface.

Demitry

 
Old November 22nd, 2005, 01:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Well, not that I am trying to defend the documentation (since IMO it has many problems) but if you look at the docs for the Dictionary.ContainsValue method, you'll see:
Quote:
quote:
This method determines equality using the default equality comparer EqualityComparer.Default for TValue, the type of values in the dictionary.
and if you then follow the entry for EqualityComparer.Default you'll see
Quote:
quote:
The Default property checks whether type T implements the System.IEquatable generic interface and if so returns an EqualityComparer that uses that implementation. Otherwise it returns an EqualityComparer that uses the overrides of Object.Equals and Object.GetHashCode provided by T.
I say it's hard to get much clearer than that.

I guess you just need to know where to look. ;)


Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old November 24th, 2005, 06:39 AM
Registered User
 
Join Date: Nov 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

U are right Jeff, probably i should have checked the online docs as well. In the documentation that ships with the .NET 2.0 beta theye didnt say anything about this interface. So- lesson learned- and from now on I'm gonna rely only on the online version of the documentation. Thanks for time.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Generics in 3.0 sikanderwaheed C# 2008 aka C# 3.0 1 May 5th, 2008 08:30 AM
NET Generics FREDYCOREA .NET Framework 3.5 1 February 21st, 2008 10:17 AM
Application of generics micmit C# 2005 0 October 12th, 2006 08:10 PM
Problem with generic collections demitry Visual Studio 2005 1 November 21st, 2005 05:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.