简体   繁体   English

C#:索引不在ListView的数组范围内

[英]C#: Index was outside the bounds of the array with ListView

I keep getting "Index was outside the bounds of the array." 我不断收到“索引超出数组范围”。 when I try to add items to a listView. 当我尝试将项目添加到listView时。

What am I doing wrong? 我究竟做错了什么?

Here is my code: 这是我的代码:

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

The error appears on this line: 错误出现在此行:

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

Note that you are using h.Length , not u.Length as a condition in your for loop. 请注意,您正在使用h.Length ,而不是u.Length作为for循环中的条件。 You are adding elements of u , not h and most probably, u.Length is smaller than h.Length and you get Exception when you try to access u[i]. 您要添加u元素,而不是h并且很可能是u.Length小于h.Length并且在尝试访问u [i]时会出现Exception。 It should be : 它应该是 :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM