37

I have a listView on my form. I want to add stuff to it durring the program is running.

This is the code I use

public void FillList(string[] Name,int[] empty,int[] Population,int[] Max,int[] Check,int size)
{
    if (this.InvokeRequired)
    {
        this.Invoke((MethodInvoker)delegate
        {
            for (int i = 0; i < size; i++)
            {
                ListViewItem item = new ListViewItem(Name[i]);

                item.SubItems.Add(empty[i].ToString); //error
                item.SubItems.Add(Population[i].ToString); //error
                item.SubItems.Add(Max[i].ToString);   //error

                if (Check != 1)
                    item.SubItems.Add("No");
                else
                    item.SubItems.Add("Yes");
                listView1.Items.Add(item);
            }
        });
    }
}

the parameter must be string and I tried .ToString, but I get this:

Argument '1': cannot convert from 'method group' to 'string'

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248
  • John this is off topic but have you considered with this method to pass in a class made up of each of the sub items to add. I've added detail in a reply below – Jiminy Apr 20 '09 at 07:07
  • 3
    Stupid close reason. It helped me in a completely different context as the method group error is quite confusing and the root cause of this error, not anything more specific to the exact details of the question – Jaloopa May 28 '19 at 10:31
  • 1
    @Jaloopa Agree completely; typical Stack Overflow and its massive overmoderation. This is an easy thing to miss for casual or new programmers in C#. The only difference between this closure and many others is that the others quite often have hundreds of upvotes for the answer instead of "only" 78. – Alan K Jan 02 '20 at 19:04

3 Answers3

109

You are missing the parentheses of the method call:

ToString()

Without the parentheses, the expression is a method group — that is, a reference to one or more overloaded methods.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
8

You forgot the parentheses.

It should be .ToString()

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Rik
  • 28,507
  • 14
  • 48
  • 67
1

John this is off topic but have you considered with this method to pass in a class made up of each of the sub items to add. So:

class ListItem 
{
 public string Name {get; set;}
 public int Empty {get; set;}
 public int Population {get; set;}
 public int Max {get; set;}
 public bool Checked {get; set;}
} 

That way you would need to have each of the items in the arrays passed in lined up. Trying to line up items in many arrays often make interfaces hard to use. Your method would look like

FillList(IList<ListItem> listItems)
{
if (this.InvokeRequired)
    {
        this.Invoke((MethodInvoker)delegate
        {
            foreach (ListItem listItem in listItems)
            {
                ListViewItem item = new ListViewItem(listItem .Name);

                item.SubItems.Add(listItem.Empty.ToString());
                item.SubItems.Add(listItem.Population.ToString());
                item.SubItems.Add(listItem.Max.ToString());

                item.SubItems.Add(listItem.Checked ? "No" : "Yes");

                listView1.Items.Add(item);
            }
        }
    }
}

I've just written this code straight in so there maybe some code cleanup required

Jiminy
  • 605
  • 10
  • 17