vb.net 指数数组的边界之外
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3765725/
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 outside the bounds of the array
提问by Infodayne
Im getting this error message randomly:
我随机收到此错误消息:
Index was outside the bounds of the array.
指数数组的边界之外。
And it points to this line:
它指向这一行:
Dim placename As String = RichTextBox1.Lines(0)
回答by TerrorAustralis
That means that your RichTextBox1
has no lines in it. Replace that with:
这意味着你的里面RichTextBox1
没有线条。替换为:
Dim placename As String
If RichTextBox1.Lines.Count() > 0 Then
placename=RichTextBox1.Lines(0)
Else
placename = String.Empty
End if
More Info:
Imagine an array as a street and each element in the array is a house. If there are 30 houses on the street, and I want to find house number 20, I start at the begining (1) and go up until I reach 20. With an array, 0 is where you start instead of 1, so an array with 30 elements, contains indexes 0-29. Now back to the street analogy. Imagine I go onto the street and ask for house number 31. That house doesnt exist because there is only 30 houses. That is effectively what the program is telling you. It is saying 'There are not enough elements in the array for me to get to the one you asked for'. So you asked for the element 0 in the array of lines, effectively saying 'Give me the first line'. Now, if there are 0 lines in the textbox, then the first line does not exist and you will get this error.
更多信息:
将数组想象成一条街道,数组中的每个元素都是一所房子。如果街上有 30 栋房子,我想找到门牌号 20,我从开头 (1) 开始,直到我到达 20。对于数组,0 是您开始的地方,而不是 1,所以数组有 30 个元素,包含索引 0-29。现在回到街头比喻。想象一下,我走到街上,要门牌号 31。那所房子不存在,因为只有 30 所房子。这实际上是程序告诉你的。它说'数组中没有足够的元素让我到达你要求的那个'。因此,您要求输入行数组中的元素 0,实际上是在说“给我第一行”。现在,如果文本框中有 0 行,则第一行不存在,您将收到此错误。
回答by Daniel G. Wilson
Index was outside the bounds of the array
指数数组的边界之外
That error message usually means that you have called for an object in the array at a location that is null, or has nothing there. It happens in cases like the following;
该错误消息通常意味着您在数组中为空的位置调用了一个对象,或者那里什么都没有。它发生在以下情况下;
myArray = [0,1,2,3];
trace(myArray[6]);
As there is nothing in the array at the index 6, it is outside the bounds. If the array is empty at the time of the call, it will give the error for an object at index 0.
由于索引 6 处的数组中没有任何内容,因此它超出了边界。如果在调用时数组为空,它将给出索引为 0 的对象的错误。
I can't tell any more than that by the amount of code that you posted. Try checking to make sure the array has been populated before that line is called.
我无法通过您发布的代码量来判断更多。尝试检查以确保在调用该行之前已填充数组。
回答by Kai Moroboshi
Maybe your SQL Reader didn't get any rows associated with the index you gave. That was the case for me; I was getting columns that weren't in the database.
也许您的 SQL Reader 没有获得与您提供的索引相关联的任何行。我就是这样;我得到了不在数据库中的列。
You could change your code as follows:
您可以按如下方式更改代码:
Dim placename As String
If RichTextBox1.Lines.Count > 0 Then placename = RichTextBox1.Lines(0)