Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Int to string: cannot convert from 'method group' to 'string' [closed]

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'

like image 382
Ivan Prodanov Avatar asked Apr 08 '09 11:04

Ivan Prodanov


3 Answers

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.

like image 112
Konrad Rudolph Avatar answered Sep 23 '22 22:09

Konrad Rudolph


You forgot the parentheses.

It should be .ToString()

like image 8
Rik Avatar answered Sep 21 '22 22:09

Rik


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

like image 1
Jiminy Avatar answered Sep 24 '22 22:09

Jiminy