C# 指数超出范围。必须是非负的并且小于集合的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11502745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Index was out of range. Must be non-negative and less than the size of the collection
提问by Itz.Irshad
Error is: Index was out of range. Must be non-negative and less than the size of the collection.
错误是:索引超出范围。必须是非负的并且小于集合的大小。
Scenario: I have a desktop application which loads XML Files and display the data in Grid. Now, I want to insert another file and want to append the data in both files. But, when I try to merge the data (I mean add the rows to DataTable which has rows of perviously opened file)...I am getting this error.
场景:我有一个桌面应用程序,它加载 XML 文件并在网格中显示数据。现在,我想插入另一个文件并希望在两个文件中附加数据。但是,当我尝试合并数据时(我的意思是将行添加到 DataTable 中,其中包含先前打开的文件行)......我收到此错误。
if (strPreviousFile != "")
{
dgvBooksDetails.DataSource = dtBooks;
int intCurrentRows = dgvBooksDetails.Rows.Count;
intBooksCounter = intBooksCounter + intCurrentRows;
for (int c = intCurrentRows; c < intBooksCounter; c++)
{
Book objBook = new Book();
objBook.ID = BookID[c];
objBook.Title = BookTitle[c];
objBook.Author = BookAuthor[c];
objBook.Genre = BookGenre[c];
objBook.Price = Double.Parse(BookPrice[c]);
objBook.PublishDate = DateTime.Parse(BookPublish_Date[c]);
objBook.Description = BookDescription[c];
dtBooks.Rows.Add(objBook.ID, objBook.Title, objBook.Author, objBook.Genre,
objBook.Price, objBook.PublishDate, objBook.Description);
}
}
How can I overcome this error?
我怎样才能克服这个错误?
采纳答案by Thorsten Dittmar
The line causing trouble is this:
引起问题的线路是这样的:
int intCurrentRows = dgvBooksDetails.Rows.Count;
You take this value as start for your loop. However, the rows collection counts from 0 to Count-1, so using Countto access a value of the rows collection causes an index out of bounds error.
您将此值作为循环的开始。但是,行集合从 0 计数到Count-1,因此使用Count访问行集合的值会导致索引越界错误。
Another thing: BookTitle, BookAuthoretc. are also indexed from 0 to Count-1(or Length-1if they are arrays). I'm not sure from what you've told us, but are you sure that these collections can be accessed by index the way you do? I mean, could it be they only contains the items to be added and thus need to be indexed from 0 to X instead of number of existing itemsto number of existing items + number of new items(that's what you do in your code)?
另一件事:BookTitle,BookAuthor等也从0索引到Count-1(或Length-1如果它们是阵列)。我不确定您告诉我们的内容,但是您确定可以像您那样通过索引访问这些集合吗?我的意思是,它们可能只包含要添加的项目,因此需要从 0 到 X 而不是number of existing itemsto索引number of existing items + number of new items(这就是您在代码中所做的)?
回答by Jon Taylor
Well it seems you have an array nlong and you are trying to access the nthor greater element within it. Indexes are 0based and so an nsized array must be accessed by indexes 0to n-1.
好吧,您似乎有一个长度为n的数组,并且您正试图访问其中的第 n 个或更大的元素。索引是基于0的,因此一个n大小的数组必须通过索引0到n-1来访问。
回答by Simon Fischer
Make sure that your arrays (BookTitle, BookAuthor, BookGenre, BookPrice, BookPublish_Date, and BookDescription) have the correct length (minimum intBooksCounter).
请确保您的阵列(BookTitle,BookAuthor,BookGenre,BookPrice,BookPublish_Date,和BookDescription)有正确的长度(最小intBooksCounter)。

