Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I get this error "MainForm.ListViewItemComparer' does not contain a constructor that takes '2' arguments" in my code

Why??




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



C#
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column != sortColumn)
            {
                sortColumn = e.Column;
                listView1.Sorting = SortOrder.Ascending;
            }
            else
            {
                if (listView1.Sorting == SortOrder.Ascending)
                    listView1.Sorting = SortOrder.Descending;
                else
                    listView1.Sorting = SortOrder.Ascending;
            }
            listView1.Sort();
            this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column, listView1.Sorting); //Here comes the error
        }
        }
    }
Posted
Updated 21-Jan-12 5:23am
v2

Well, the clue is in the error message...
"does not contain a constructor that takes '2' arguments"
C#
public ListViewItemComparer(int column)
Only has one argument.
this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column, listView1.Sorting);
Has two arguments.
You need to add a constructor taking a SortOrder parameter as well as the Column.
 
Share this answer
 
Comments
Goran Shoan 21-Jan-12 11:40am    
Thanks for helping. How can i make my constructor taking the SortOrder
OriginalGriff 21-Jan-12 11:52am    
public ListViewItemComparer(int column, SortOrder sortOrder)
{
...
}
Goran Shoan 21-Jan-12 11:53am    
Thanks alot
Sergey Alexandrovich Kryukov 21-Jan-12 13:53pm    
Aha, a 5.
--SA
[Updated thanks to Griff]

Well you are calling the constructor with two arguments
Goran Shoan wrote:
new ListViewItemComparer(e.Column, listView1.Sorting);
while providing only zero
Goran Shoan wrote:
public void ListViewComparer()
and
one argument
Goran Shoan wrote:
public ListViewItemComparer(int column)
constructors. Of course the compiler cannot accept that.

You have either to add the required two arguments constructor or modify the call for invoking the one of the already available ones.
 
Share this answer
 
v2
Comments
OriginalGriff 21-Jan-12 11:53am    
Oops?
public void ListViewComparer()
Is not a constructor... :laugh:
Sergey Alexandrovich Kryukov 21-Jan-12 13:52pm    
Right.
--SA
CPallini 21-Jan-12 17:24pm    
Right. :-O
Thank you.
Sergey Alexandrovich Kryukov 21-Jan-12 13:53pm    
Perhaps "pre" tag would be appropriate, instead of "blockquote".
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900