2012-11-17 104 views
0

我總是收到「索引超出數組邊界」。當我嘗試將項目添加到列表視圖。C#:索引超出了數組範圍的邊界ListView

我在做什麼錯?

這裏是我的代碼:

string[] h = getBetweenAll(thepage, "\" target=\"_blank\">", "</a>"); 
     foreach (string s in h) 
     listViewClickbank.Items.Add(new ListViewItem("")); 

     foreach (ListViewItem i in listViewClickbank.Items) 
     { 
      if (i.SubItems[0].Text == "(view mobile)") 
      { 
       i.Remove(); 
      } 
     } 

     foreach (ListViewItem i in listViewClickbank.Items) 
    { 
      if (i.SubItems[0].Text.Contains("recordTitle")) 
      { 
      i.Remove(); 
      } 
     } 

     string[] u = getBetweenAll(thepage, "<div class=\"description\">", "</div>"); 
     for (int i = 0; i < h.Length && i < listViewClickbank.Items.Count; i++) 
     { 
      listViewClickbank.Items[i].SubItems.Add(u[i]); 
     } 

在這條線出現錯誤:

listViewClickbank.Items[i].SubItems.Add(u[i]); 

回答

1

請注意,您所使用h.Length,不u.Length在你for循環的條件。您正在添加u的元素,而不是h,最有可能的是,u.Length小於h.Length,並且當您嘗試訪問u [i]時,您將獲得Exception。它應該是:

string[] u = getBetweenAll(thepage, "<div class=\"description\">", "</div>"); 
for (int i = 0; i < u.Length && i < listViewClickbank.Items.Count; i++) 
{ 
    listViewClickbank.Items[i].SubItems.Add(u[i]); 
} 
+0

謝謝!有效 :) –