473,397 Members | 2,116 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,397 software developers and data experts.

Contains / Equals

I have a collection class that contains objects of the type of another class
call it Class1, that I have created. The constructor for Class1 takes two integers
These go on to be fields of the class, 'Low' and 'High'

I want the following to happen
MyCollection cn = new MyCollection()
cn.Add(new Class1(1, 2))
cn.Add(new Class1(1, 3))
cn.Add(new Class1(1, 4))
//then I want this to be true
if(cn.Contains(new Class1(1, 3))

//object with Low = 1, High = 3 has been foun
Of course they are reference types, so they are no
the same thing, and it returns false. But in my mind the
are, so I want to be able to program it so that it knows this

I'm pretty sure from the documentation that it is possibl
to determine this using some fancy hash-table based method
and I'd rather do this than a loop
although it is a bit of a minefield and I'm fairly new to this
What do I do - override Class1.Equals()? Overrid
MyCollection.Contains()? Implement IComparable
All of these
I don't need the objects to be ordered, although I do nee
them to behave like value types, but only with regard to th
Contains method
Can anyone give me a starter

Thank

Nov 16 '05 #1
5 8834
You would have to override the == operator to implement you own:
public static bool operator == ( Class1 a, Class1 b )
{

return a.Low == b.Low && a.High == b.High;

}

Then in MyCollection, in the Contains method, you step through the items
to check the equality.

"songie D" <an*******@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...
I have a collection class that contains objects of the type of another class, call it Class1, that I have created. The constructor for Class1 takes two integers. These go on to be fields of the class, 'Low' and 'High'.

I want the following to happen:
MyCollection cn = new MyCollection();
cn.Add(new Class1(1, 2));
cn.Add(new Class1(1, 3));
cn.Add(new Class1(1, 4));
//then I want this to be true:
if(cn.Contains(new Class1(1, 3)))
{
//object with Low = 1, High = 3 has been found
}

Of course they are reference types, so they are not
the same thing, and it returns false. But in my mind they
are, so I want to be able to program it so that it knows this.

I'm pretty sure from the documentation that it is possible
to determine this using some fancy hash-table based method,
and I'd rather do this than a loop.
although it is a bit of a minefield and I'm fairly new to this.
What do I do - override Class1.Equals()? Override
MyCollection.Contains()? Implement IComparable?
All of these?
I don't need the objects to be ordered, although I do need
them to behave like value types, but only with regard to the
Contains method.
Can anyone give me a starter?

Thanks

Nov 16 '05 #2
songie D <an*******@discussions.microsoft.com> wrote:
I have a collection class that contains objects of the type of another class,
call it Class1, that I have created. The constructor for Class1 takes two integers.
These go on to be fields of the class, 'Low' and 'High'.

I want the following to happen:
MyCollection cn = new MyCollection();
cn.Add(new Class1(1, 2));
cn.Add(new Class1(1, 3));
cn.Add(new Class1(1, 4));
//then I want this to be true:
if(cn.Contains(new Class1(1, 3)))
{
//object with Low = 1, High = 3 has been found
}

Of course they are reference types, so they are not
the same thing, and it returns false. But in my mind they
are, so I want to be able to program it so that it knows this.

I'm pretty sure from the documentation that it is possible
to determine this using some fancy hash-table based method,
and I'd rather do this than a loop.
although it is a bit of a minefield and I'm fairly new to this.
What do I do - override Class1.Equals()? Override
MyCollection.Contains()? Implement IComparable?
All of these?


All you need to *really* do is override Class1.Equals, but you should
also override Class1.GetHashCode for two reasons:

1) To avoid getting a warning every time you compile :)
2) To allow you use the class as a key in a hashtable

Now how you implement Contains in your collection will depend on how
the collection works in general. An ArrayList would just use a loop, a
hashtable would basically look up the key in the normal way.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
songie D <an*******@discussions.microsoft.com> wrote:
how would I implement GetHashCode to produce a single unique integer out of
two unique integers?


You don't have to - it doesn't need to be unique. It's just preferable
for the same result not to crop up *too* often for different objects.

You could either XOR it (result = a^b) or (as suggested in an excellent
Java book when constructing a hashcode from potentially many others)
use something like result = a*17+b. (The exact details are in the book
- it's the one by Josh Bloch, Elements of Java Style IIRC.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
songie D <an*******@discussions.microsoft.com> wrote:
OK, thanks Jon.
I've found that if I implement Equals (which means I have to implement GetHashCode)
and operator== (which means I have to implement operator!=) then Contains
works the way I want it to without me overriding it.
I had overridden it, so I didn't need it - so I thought I'd delete the method so it
just used the base class's one instead. But when I had, the base class apparently
didn't have a Contains method. Is this normal? Should it have a Contains method?


What is the base class in this case?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
songie D <an*******@discussions.microsoft.com> wrote:
It's a CollectionBase.

I started a new project, and added a class derived from CollectionBase. But it too didn't
implicitly 'inherit' Contains from CollectionBase, so I presume it's not supposed to.

Or is it?


CollectionBase implements IList.Contains explicitly - so you can't use
it directly in your code without casting to IList, but it *is* there.

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

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

Similar topics

3
by: Ton van den Heuvel | last post by:
Hi, I just noticed that ArrayList.Contains(Object obj) uses the Equals method of the object that is passed to the Contains method to check for equality with the object in the list. Wouldn't it...
2
by: Piotr Szukalski | last post by:
Hi! I have trouble with 'Contains' method in ListViewItemCollection class - it seems like it nevers calls 'Equals' method of class inherited from ListViewItem... I've found that ListViewItem...
4
by: Robert W. | last post by:
I've built a complex collection object from "System.Collections.CollectionBase". With regard to it, I have a question about the "Contains" method. At first I thought that "Contains" would...
5
by: matt tagliaferri | last post by:
I'm implementing a typed collection class that can't include duplicates. The Add method of the collection class is as follows: Public Function Add(ByVal o As TeamListEntry) As Integer If...
2
by: Dot net work | last post by:
Hello, My simple code is here: Public Class MyDictionary Inherits System.Collections.DictionaryBase Private Class MyElement Public Overloads Overrides Function Equals(ByVal obj As Object)...
3
by: JB | last post by:
I've created a generic of type List<T> where T is a custom class. I need to use the List<T>.Contains method. I know I need to implement the IEqualityComparer but I can't seem to get the Contains...
8
by: HarvSather | last post by:
C# VS2005 I have a class with 5 properties I am adding instances this class to an ArrayList I want to test to see if an instance is already in the ArrayList before attempting to add the...
8
by: semedao | last post by:
Hi I have some Queue object that I enqueue other objects into it. I want to check if someobject already exists before enqueue , using the Contains method but italways return false. in the object...
12
by: Robert Hooker | last post by:
We are moving to .NET2.0 - and have noticed that .Contains() seems to be around 20% slower under NET2, compared to NET1.1. This code shows the issue: static void Main(string args) {...
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?
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.