简体   繁体   English

InvalidArgument =值'6'对于索引无效。 参数名称:索引

[英]InvalidArgument=Value of '6' is not valid for index. Parameter name: index

I created a Listview control with 8 columns. 我创建了一个具有8列的Listview控件。 When I need to retrieve text from subitem of Item, I use the following code: 当我需要从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;
                }
            }

it shows an exception. 它显示了一个异常。 But when I replace item.SubItems[6].Text with item.SubItems[5].Text it works. 但是,当我用item.SubItems [5] .Text替换item.SubItems [6] .Text时,它可以工作。 How can I fix this? 我怎样才能解决这个问题?

SubItems[6]

6它表示列索引而不是行的索引。

显然,SubItems中最多有6列

If you've only created 6, then the values are 0-5; 如果仅创建6,则值为0-5;否则,值为0。 meaning 6 is invalid. 意思是6是无效的。

This is a classic "off-by-one" error scenario. 这是经典的“一站式”错误方案。

Indicies come in two flavors: Zero-based, and One-based. 指标有两种:基于零的和基于一的。 C# is a zero-based index language. C#是从零开始的索引语言。 I assume you're either learning a language for the first time, or learning a zero-based language for the first time - Otherwise, I'm missing the point of the question, and I apologize. 我假设您是第一次学习某种语言,还是第一次学习一种从零开始的语言-否则,我错过了问题的重点,我深表歉意。 :) :)

See Wikipedia, Off-By-One Error: http://en.wikipedia.org/wiki/Off-by-one_error 参见Wikipedia,一次性错误: http//en.wikipedia.org/wiki/Off-by-one_error

The ArgumentOutOfRange exception is thrown by the runtime when it realizes that there is no 7th item in the list. 当运行时运行器意识到列表中没有第七项时,将引发ArgumentOutOfRange异常。 This kind of error cannot be caught at compile time (without using heuristics), due to the fact that the list may contain any number of values at any time 由于列表随时可能包含任意数量的值,因此无法在编译时捕获此类错误(不使用启发式方法)

TLRD; TLRD;

Zero-Based (C#): 从零开始(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.

One-Based (some other language): 基于一词(其他语言):

... = 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.

相关问题 InvalidArgument =值'3'对于'索引'无效。 参数名称:索引 - InvalidArgument=Value of'3' is not valid for 'index'. parameter name: index InvalidArgument='-1' 的值对 'index' 无效。 参数名称:索引 - InvalidArgument=Value of '-1' is not valid for 'index'. Parameter name: index invalidargument =值'8'对于'索引'无效 - invalidargument=value of '8' is not valid for 'index' InvalidArgument =值'4'对于'索引'无效 - InvalidArgument=Value of '4' is not valid for 'index' InvalidArgument =值'1'对'索引'无效 - InvalidArgument=Value of '1' is not valid for 'index' “InvalidArgument=‘1’的值对‘index’无效。 参数名称:index”,显示第二个数据索引并插入数据库时出现 - “InvalidArgument=Value of '1' is not valid for 'index'. Parameter name: index”, occurs when the second data index is displayed and nserted to database 附加信息:InvalidArgument =值“ 0”对“索引”无效 - Additional information: InvalidArgument=Value of '0' is not valid for 'index' C# InvalidArgument = '2' 的值对于 'index' 无效 - C# InvalidArgument = Value of '2' is not valid for 'index' ListView中的错误:InvalidArgument =值'0'对'index'无效 - Error in ListView: InvalidArgument = Value of '0' is not valid for 'index' Listview.count-InvalidArgument =值“ 0”对“索引”无效 - Listview.count - InvalidArgument=Value of '0' is not valid for 'index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM