Question

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'

Was it helpful?

Solution

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.

OTHER TIPS

You forgot the parentheses.

It should be .ToString()

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top