473,748 Members | 10,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overriding Compare/ListViewItemSor ter

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 ListViewItemSor ter.

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(DBSearch Info)
{
DBSearchInfo dbiX = x as DBSearchInfo;
DBSearchInfo dbiY = y as DBSearchInfo;
return dbiX.GetHashCod e() - dbiY.GetHashCod e();
}
else if (x.GetType() == typeof(ListView Item)
{
DBSearchInfo lviX = x as ListViewItem;
DBSearchInfo lviY = y as ListViewItem;
return Compare(lviX.Ta g, lviY.Tag); // Tag is known to be a
DBSearchInfo item
}
else
{
return 0;
}

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

// In another class....
private void AddStuffToList( CClass1 Class1)
{
lvwSearchResult s.ListViewItemS orter = new DBSearchInfo();

// |
// Some other code
// |

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

DBSearchInfo ThisTag = new DBSearchInfo(Cl ass1,Class2);
ListViewItem lItem = new ListViewItem(Cl ass2.ItemName);

lItem.SubItems. Add(Class2.Shor tName);
lItem.SubItems. Add(Class1.Grou pName);

lItem.Tag = ThisTag;

// This is where the problem is
if (! lvwSearchResult s.Items.Contain s(lItem))
lvwSearchResult s.Items.Add(lIt em);
}
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 3331
pagates wrote:

// This is where the problem is
if (! lvwSearchResult s.Items.Contain s(lItem))
lvwSearchResult s.Items.Add(lIt em);
}
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
PowerCollection s) 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 (! lvwSearchResult s.Items.Contain s(lItem))
lvwSearchResult s.Items.Add(lIt em);

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
PowerCollection s) 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
3751
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, quarter=1) >>> 2001q4 = qDate(year=2001, quarter=4) >>> 2001q4 - 2001q1
0
1259
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 of the list are equal or not. In my specific case a number of the list might be equivalent with another number of the list yet their values would be different. Any idea as to how what and how I can override or any workaround for this problem?...
3
4195
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 the documentation but it is rather hard to understand so please don't refer to it :) 3) Many examples in the newsgroups use Return MyBase.ProcessKeyPreview(m) as the last code line while I have used Return MyBase.ProcessKeyEventArgs(m)
17
2915
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 when the Poodle is upcast to the Dog (in its wildest dreams) it then says "bow wow" where the bernard always says "woof" (see code). Basically, it appears that I'm hiding the poodle's speak method from everything except the poodle. Why would I...
12
6751
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 True. Therefore, overriding the Equals method in the class definition of the items I put in the ArrayList should make the IndexOf method use my version of the Equals method?!
3
1553
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
4743
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
3
223
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 == and != opperators. Am I missing something or when would one want to have different implementations for Equals and ==? --Ken
10
105192
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
0
9363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.