2009-04-08 89 views
12

我在窗體上有一個listView。我想在程序運行期間添加東西。Int to string:無法從'method group'轉換爲'string'

這是我的代碼使用

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); 
      } 
     }); 
    } 
} 

的參數必須是字符串,我試圖.ToString,但我得到這個:

參數「1」:無法從「方法組」轉換以 '字符串'

+0

約翰這是題外話,但你用這種方法考慮由一個類來傳遞要添加的每個子項目。我在下面的回覆中添加了詳細信息 – Jiminy 2009-04-20 07:07:46

回答

57

你錯過了括號:

ToString()

8

你忘了括號。

應該.ToString()

0

約翰這是題外話,但你用這種方法考慮由各個子項中添加的一個類來傳遞。所以:

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;} 
} 

這樣你就需要讓數組中的每個項都排成隊列。嘗試在許多陣列中排列項目往往使界面難以使用。你的方法看起來就像

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); 
      } 
     } 
    } 
} 

我剛剛寫了這個代碼直所以也許有些代碼清理需要