![](/img/trans.png)
[英]InvalidArgument=Value of'3' is not valid for 'index'. parameter name: index
[英]InvalidArgument=Value of '6' is not valid for index. Parameter name: index
我创建了一个具有8列的Listview控件。 当我需要从Item的子项中检索文本时,我使用以下代码:
foreach (ListViewItem item in listViewStatus.Items)
{
if (item.Tag == f)
{
/* Use locking to synchronise across mutilple thread calls. */
lock (_lockObject)
{
item.SubItems[6].Text = Status;
}
break;
}
}
它显示了一个异常。 但是,当我用item.SubItems [5] .Text替换item.SubItems [6] .Text时,它可以工作。 我怎样才能解决这个问题?
SubItems[6]
6
它表示列索引而不是行的索引。
显然,SubItems中最多有6列
如果仅创建6,则值为0-5;否则,值为0。 意思是6是无效的。
这是经典的“一站式”错误方案。
指标有两种:基于零的和基于一的。 C#是从零开始的索引语言。 我假设您是第一次学习某种语言,还是第一次学习一种从零开始的语言-否则,我错过了问题的重点,我深表歉意。 :)
参见Wikipedia,一次性错误: http : //en.wikipedia.org/wiki/Off-by-one_error
当运行时运行器意识到列表中没有第七项时,将引发ArgumentOutOfRange异常。 由于列表随时可能包含任意数量的值,因此无法在编译时捕获此类错误(不使用启发式方法)
TLRD;
从零开始(C#):
... = myList[0]; // This is a zero-based indexer.
... = myList[1];
... = myList[2];
... = myList[3];
... = myList[4];
... = myList[5]; // This is the 6th item, although the index is 5.
基于一词(其他语言):
... = myList[1]; // This is a one-based indexer.
... = myList[2];
... = myList[3];
... = myList[4];
... = myList[5];
... = myList[6]; // This is the 6th item, and the index is 6.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.