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

Overriding Compare/ListViewItemSorter

Hello Gurus,

I have a listview, and I only want to add unique items. The problem is, my
code is blowing by my "Contains" statement, adding the item, and then hitting
my Compare code in the class that I have set up for my ListViewItemSorter.

Here is my (simplified) code:

// Note: there are accessor properties, constructors, etc
// that I have omitted here for clarity
public class DBSearchInfo : IComparer
{
// Class1 and Class2 each have a unique ID property
private CClass1 ThisClass1;
private CClass2 ThisClass2;

#region IComparer Members
public int Compare(object x, object y)
{
if (x.GetType() == typeof(DBSearchInfo)
{
DBSearchInfo dbiX = x as DBSearchInfo;
DBSearchInfo dbiY = y as DBSearchInfo;
return dbiX.GetHashCode() - dbiY.GetHashCode();
}
else if (x.GetType() == typeof(ListViewItem)
{
DBSearchInfo lviX = x as ListViewItem;
DBSearchInfo lviY = y as ListViewItem;
return Compare(lviX.Tag, lviY.Tag); // Tag is known to be a
DBSearchInfo item
}
else
{
return 0;
}

public override int GetHashCode()
{
string hashValue = ThisClass1.ID.ToString() +
":" + ThisClass2.ID.ToString();
return hashValue.GetHashCode();
}
}
#endregion
}

// In another class....
private void AddStuffToList(CClass1 Class1)
{
lvwSearchResults.ListViewItemSorter = new DBSearchInfo();

// |
// Some other code
// |

while (MyReader.Read())
{
CClass2 Class2=
new CClass2().GetClass2FromReader(MyReader);

DBSearchInfo ThisTag = new DBSearchInfo(Class1,Class2);
ListViewItem lItem = new ListViewItem(Class2.ItemName);

lItem.SubItems.Add(Class2.ShortName);
lItem.SubItems.Add(Class1.GroupName);

lItem.Tag = ThisTag;

// This is where the problem is
if (! lvwSearchResults.Items.Contains(lItem))
lvwSearchResults.Items.Add(lItem);
}
MyReader.Close();
}

When I hit the "Contains" statement above, it always goes to the "Add"
statement, and then I hit a breakpoint in the Compare function. Everything
in the Compare code seems to work correctly, but too late.

Have I implemented the wrong interface for the Contains test?

Thanks,
pagates
Nov 17 '05 #1
3 3309
pagates wrote:

// This is where the problem is
if (! lvwSearchResults.Items.Contains(lItem))
lvwSearchResults.Items.Add(lItem);
}
MyReader.Close();
}

When I hit the "Contains" statement above, it always goes to the "Add"
statement, and then I hit a breakpoint in the Compare function. Everything
in the Compare code seems to work correctly, but too late.

Have I implemented the wrong interface for the Contains test?


I don't think there's an interface that needs to, or can, be implemented
to support Contains in this context. I had a quick look into Reflector
and it doesn't look like the list view items collection would ever use
any external implementation to find out whether a given item is already
in the collection. What gave you the idea that it should do this?

You could try to store your own Hashtable (or Set, if you use
PowerCollections) of the item contents that are already in the list.
It's only a reference, so it shouldn't take up too many resources, and
lookup in a Hashtable or other specialized collection class is fast.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #2
"Oliver Sturm" wrote:
// This is where the problem is
if (! lvwSearchResults.Items.Contains(lItem))
lvwSearchResults.Items.Add(lItem);

When I hit the "Contains" statement above, it always goes to the "Add"
statement, and then I hit a breakpoint in the Compare function. Everything
in the Compare code seems to work correctly, but too late.

Have I implemented the wrong interface for the Contains test?


I don't think there's an interface that needs to, or can, be implemented
to support Contains in this context. I had a quick look into Reflector
and it doesn't look like the list view items collection would ever use
any external implementation to find out whether a given item is already
in the collection. What gave you the idea that it should do this?

You could try to store your own Hashtable (or Set, if you use
PowerCollections) of the item contents that are already in the list.
It's only a reference, so it shouldn't take up too many resources, and
lookup in a Hashtable or other specialized collection class is fast.

Oliver Sturm


Hi Oliver,

Thanks for the reply - I've implemented the functionality I've wanted by
using a global ArrayList (for now, unless speed becomes an issue).

I wanted to make sure that the list items were unique. The list is a
collection of search results on two types of related data, and I didn't want
to duplicate entries in the list. I would have thought that the Contains
method would allow me to check for a duplicate item. Another example may be
handling Drag and Drop, preventing duplicate items from being dropped onto
the list.

I am guessing that because I am checking a new ListViewItem, rather than an
existing ListViewItem, that it fails. However, in that case, what is the
purpose of the Contains method? If I need to use an existing ListViewItem, I
know it exists, so it is therefore contained in the collection. Anybody have
an explination or example of "properly" using the Contains method for
ListViewItems?

Thanks Again,
pagates
Nov 17 '05 #3
pagates wrote:
I am guessing that because I am checking a new ListViewItem, rather than an
existing ListViewItem, that it fails.
Right, and because the Contains doesn't use the IComparer interface to
check equality of objects.
However, in that case, what is the
purpose of the Contains method? If I need to use an existing ListViewItem, I
know it exists, so it is therefore contained in the collection. Anybody have
an explination or example of "properly" using the Contains method for
ListViewItems?


Well, for one thing the ListViewItems implement the interface IList and
this interface defines the Contains method. So the implementor of that
specific collection type didn't really have a choice of whether or not
to implement the method.

The method in general serves the purpose of finding out whether a given
object is in a collection or not. You could have loads of objects of a
given type and several collections, so there are a number of reasons why
you might want to know if a given object is in a given collection.

You are probably right that it doesn't make as much sense in a simple
scenario with a single list view :-)

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #4

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

Similar topics

6
by: python | last post by:
Hi- I need to make a class for quartlerly dates. I need to be able to compare two quarterly dates and get the number of quarters between them. For example: >>> 2001q1 = qDate(year=2001,...
0
by: Peter Galfi | last post by:
I am trying to make a compare between two lists of numbers using the SequenceMatcher, however I would need somehow to override the method which is used by SequenceMatcher to determine if two elements...
3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
3
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
3
by: Kenneth Baltrinic | last post by:
When one overrides the Equals() method of an object, one is supposed to override GetHashCode() as well and this makes good sense. But I have seen lots of people who do this and do not override the...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.