472,342 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

Using List.Contains()

I feel like I'm about to get in over my head, so before I go off the
deep end I'm hoping someone can point me in the right direction.

I started down the road of using a List<> of an Interface type.

I wanted to be able to use List.Contains() to determine if the List
already has a certain object based on the Name property - which is
defined by the Interface.

Well it didn't work at first, so I read up and found that I need to
override the Equals and == methods. That was pretty easy, but then I
got warnings that I need to override GetHashCode.

Now, here's my fear:

I want to compare based on Name, but Name is a get-able and set-able
property of the classes that implement ISample. There are quite a few
warnings about how the hash code can never change. But with Name being
set-able, I can't be sure it won't change.

I need Name to be get/set-able because the class is deserialized and I
need a parameterless constructor to do that.
So what do I do??? Thanks for any help...

--Brian
Nov 23 '05 #1
1 10267
Brian Pelton <no@email.com> wrote:
I feel like I'm about to get in over my head, so before I go off the
deep end I'm hoping someone can point me in the right direction.

I started down the road of using a List<> of an Interface type.

I wanted to be able to use List.Contains() to determine if the List
already has a certain object based on the Name property - which is
defined by the Interface.

Well it didn't work at first, so I read up and found that I need to
override the Equals and == methods. That was pretty easy, but then I
got warnings that I need to override GetHashCode.

Now, here's my fear:

I want to compare based on Name, but Name is a get-able and set-able
property of the classes that implement ISample. There are quite a few
warnings about how the hash code can never change. But with Name being
set-able, I can't be sure it won't change.

I need Name to be get/set-able because the class is deserialized and I
need a parameterless constructor to do that.


The documentation for GetHashCode makes it virtually impossible to
implement in a meaningful way for mutable objects. Instead of following
the documentation precisely, I would implement GetHashCode so that the
hash code doesn't change while equality doesn't change - in other
words, make sure that if x.Equals(y), then
x.GetHashCode()==y.GetHashCode().

If someone chooses to change the Name of one of your objects while it's
being used as a key in a Hashtable, then they're not going to be able
to find it - but you can make sure that so long as the object *isn't*
changed, it can work as a hash key.

Not everything is naturally a good key for a hashtable, unfortunately -
I believe that making GetHashCode a method on Object rather than
creating an IHashable interface was a mistake, personally. (I'm still
somewhat on the fence about it, admittedly...)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 23 '05 #2

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

Similar topics

3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on...
9
by: TheOne | last post by:
Would anyone please point me to a list of reentrant C library functions? I want to know which C library functions are safe to use inside a signal...
2
by: sianan | last post by:
I am having a problem doing the following in generics. I have two list of a custom item type. I need to iterate through the first list and match...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and...
19
Atli
by: Atli | last post by:
Introduction At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class...
2
by: Jeff | last post by:
..NET 2.0 I'm trying to use List.Contains to determine if an object already exist in the list. This Contains method always return false in my...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.