C# 整数到字符串:无法从“方法组”转换为“字符串”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/729642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:52:46  来源:igfitidea点击:

Int to string: cannot convert from 'method group' to 'string'

c#winformsstringlistviewint

提问by Ivan Prodanov

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

我的表单上有一个 listView。我想在程序运行期间向其中添加内容。

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:

参数必须是字符串,我试过了.ToString,但我得到了这个:

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

参数“1”:无法从“方法组”转换为“字符串”

采纳答案by Konrad Rudolph

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.

没有括号,表达式是一个方法组?——即对一个或多个重载方法的引用。

回答by Rik

You forgot the parentheses.

你忘记了括号。

It should be .ToString()

它应该是 .ToString()

回答by Jiminy

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

我刚刚直接写了这段代码,所以可能需要一些代码清理