G
Guest
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
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