[英]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.