Question about sorting


Z

Zach

In the code below, where indicated error 1, I get this error message: Error
1 using the generic type 'System.Collections.Generic.IComparer<T>' requires
'1' type arguments.

What I really want to do is use CODE, WITHOUT MANUALLY selecting column
three, to sort column three, and get the rest of listview in column three
order.

Could s.o. please tell me what to do?

Many thanks,
Zach.


#region Sorting columns.
// Implements the manual sorting of items by columns.

class ListViewItemComparer : IComparer <------- error 1
{
private int col;
public ListViewItemComparer()
{
col = 0;
}
public ListViewItemComparer(int column)
{
col = column;
}
public int Compare(object x, object y)
{
return String.Compare(((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
}
}

// ColumnClick event handler.
private void ColumnClick(object o, ColumnClickEventArgs e)
{
// Set the ListViewItemSorter property to a new
ListViewItemComparer object.
this.listView1.ListViewItemSorter = new
ListViewItemComparer(e.Column);
// Call the sort method to manually sort the column based on the
ListViewItemComparer implementation.
listView1.Sort();
}
#endregion
 
Ad

Advertisements

H

Harlan Messinger

Zach said:
In the code below, where indicated error 1, I get this error message:
Error 1 using the generic type 'System.Collections.Generic.IComparer<T>'
requires '1' type arguments.

Right. The type name is IComparer<T>, not IComparer, as you have it. You
can either implement this for a specific type, as in:

class ListViewItemComparer : IComparer<string>
{
public int Compare(string x, string y)
...

or you can make your implementing class generic:

class ListViewItemComparer<T> : IComparer<T>
{
public int Compare(T x, T y)
...

depending on which of these represents what you are trying to accomplish.

[snip]
#region Sorting columns.
// Implements the manual sorting of items by columns.

class ListViewItemComparer : IComparer <------- error 1
{
[snip]
 
P

Peter Duniho

Zach said:
In the code below, where indicated error 1, I get this error message:
Error 1 using the generic type 'System.Collections.Generic.IComparer<T>'
requires '1' type arguments.

Just as the error is explaining to you, the use of the
System.Collections.Generic.IComparer<T> type requires one type argument.
You haven't provided one.

If you want to implement System.Collections.IComparer instead, then
either don't put "using System.Collections.Generic;" as a "using"
directive in your source file, or fully-qualify the type name as
"System.Collections.IComparer".

If you do want to implement System.Collections.Generic.IComparer<T>,
then you have to provide a type parameter for the type (i.e.
"IComparer<ListViewViewItem>") and then implement that interface.

Either way will work fine (though IMHO it is much better to implement
the generic interface). But you do need to pick one or the other; it
won't work if you have half your code trying to do one, and half your
code trying to do the other. :)

Pete
 
P

Peter Duniho

Zach said:
I added using System.Collections; and the code as I had it worked fine.

Yup, that would yet another way to make sure your class was declared as
implementing the interface you really wanted to implement.
 
Ad

Advertisements

Z

Zach

Thank you all.
Zach.

Peter Duniho said:
Yup, that would yet another way to make sure your class was declared as
implementing the interface you really wanted to implement.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top