System.ArgumentOutOfRangeException 是什么意思?(vb.net)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15704555/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:02:12  来源:igfitidea点击:

What does the System.ArgumentOutOfRangeException mean? (vb.net)

vb.netfirst-chance-exception

提问by Hemkesh Molla

I am having the 'A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll'exception and it is affecting my program. Here is my code. Below it is my textual summary of the codes.

在 mscorlib.dll异常中发生了“System.ArgumentOutOfRangeException”类型的第一次机会异常,它正在影响我的程序。这是我的代码。下面是我对代码的文本摘要。

Private Sub Group_LeaderTextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Group_LeaderTextBox.LostFocus
    Dim ListCounter As Integer
    Dim Checker As Integer = 0
    Dim NewString As String

    If Group_LeaderTextBox.Text <> "" Then
        If ListBox_ParticipantsNames.Items.Count = 0 Then
            ListBox_ParticipantsNames.Items.Add(Group_LeaderTextBox.Text & " (Group Leader)")
        Else
            For ListCounter = 0 To ListBox_ParticipantsNames.Items.Count - 1
                If ListBox_ParticipantsNames.Items.Item(ListCounter).ToString.Substring(0) = Group_LeaderTextBox.Text Then
                    If ListBox_ParticipantsNames.Items.Item(0).ToString.Substring(ListBox_ParticipantsNames.Items.Item(0).ToString.Length - 14) = "(Group Leader)" Then
                        NewString = ListBox_ParticipantsNames.Items.Item(0).ToString.Replace(" (Group Leader)", "")
                        ListBox_ParticipantsNames.Items.RemoveAt(0)
                        ListBox_ParticipantsNames.Items.Insert(0, NewString)
                    End If
                    ListBox_ParticipantsNames.Items.RemoveAt(ListCounter)
                    ListBox_ParticipantsNames.Items.Insert(0, Group_LeaderTextBox.Text & " (Group Leader)")
                    Exit For
                Else
                    Checker += 1
                End If
            Next

            If Checker = ListBox_ParticipantsNames.Items.Count Then
                If ListBox_ParticipantsNames.Items.Item(0).ToString.Substring(ListBox_ParticipantsNames.Items.Item(0).ToString.Length - 14) = "(Group Leader)" Then
                    ListBox_ParticipantsNames.Items.RemoveAt(0)
                    ListBox_ParticipantsNames.Items.Insert(0, Group_LeaderTextBox.Text & " (Group Leader)")
                Else
                    ListBox_ParticipantsNames.Items.Insert(0, Group_LeaderTextBox.Text & " (Group Leader)")
                End If
            End If
        End If
    Else
        If ListBox_ParticipantsNames.Items.Item(0).ToString.Substring(ListBox_ParticipantsNames.Items.Item(0).ToString.Length - 14) = "(Group Leader)" Then
            ListBox_ParticipantsNames.Items.RemoveAt(0)
        End If
    End If
End Sub

So this code is for adding values to a list box when the text box 'Group_LeaderTextBox' (Lets call it 'X') has lost focus.

因此,此代码用于在文本框“Group_LeaderTextBox”(我们称之为“X”)失去焦点时向列表框添加值。

The logic is this (in order of the codes written):

逻辑是这样的(按编写的代码顺序):

But first, some prerequisites:

但首先,一些先决条件:

  • There are 2 ways to add items to the list box, one is via X (on it losing focus), and second is via another text box (Let's call it 'Y'). (Not important)
  • All text from X, added to the list box, appears first in the list box, no matter what is already present in the list box.
  • The text from X, say "John", when added to the list box, will appear with the added string " (Group Leader)". So, on losing focus, "John" will appear as "John (Group Leader)". Remember that there can be input from Y too, however input from Y is simply the text that it contains.
  • If (on losing focus) X is empty, then if the first item in the list box contains the substring "(Group Leader)", it will be deleted. So, if "John" is removed from X, "John (Group Leader)" will be removed. And if there is no group leader, nothing will happen.
  • 有两种方法可以将项目添加到列表框中,一种是通过 X(失去焦点),第二种是通过另一个文本框(我们称之为“Y”)。(不重要)
  • X 中添加到列表框中的所有文本首先出现在列表框中,无论列表框中已经存在什么内容。
  • 来自 X 的文本,例如“John”,当添加到列表框时,将显示添加的字符串“(组长)”。因此,在失去焦点时,“约翰”将显示为“约翰(组长)”。请记住,也可以从 Y 输入,但是来自 Y 的输入只是它包含的文本。
  • 如果(失去焦点时)X 为空,那么如果列表框中的第一项包含子字符串“(组长)”,则将其删除。因此,如果从 X 中删除“John”,则将删除“John (Group Leader)”。如果没有组长,什么都不会发生。

Logic:

逻辑:

Suppose I have entered "John" in X. On focus lost, if the list box is empty, "John (Group Leader)" will simply be added to the list box. Else, if for e.g. I had already added some items to the list box via Y, the program will check whether there already is "John" in the list box. If there is, and there is no group leader already present in the list, item "John" will be replaced with "John (Group Leader)" added onto the top of the list (this is not working). And if there is already a group leader, "John (Group Leader)" is still gonna appear at the top, but the previous group leader will be demoted. So, if previously "Dick" :P was the previous group leader, the string "(Group Leader)" will be removed from "Dick (Group Leader)".

假设我在 X 中输入了“John”。在失去焦点时,如果列表框为空,“John (Group Leader)”将简单地添加到列表框中。否则,如果例如我已经通过 Y 向列表框中添加了一些项目,程序将检查列表框中是否已经有“John”。如果有,并且列表中没有组长,则项目“John”将替换为添加到列表顶部的“John(组长)”(这不起作用)。如果已经有组长,“约翰(组长)”仍然会出现在顶部,但之前的组长将被降级。因此,如果之前的“Dick”:P 是之前的组长,则字符串“(Group Leader)”将从“Dick (Group Leader)”中删除。

Now, if after checking the whole list, there is no item that matches "John", then the program will check, as before, whether there is already a group leader. If there is, then the previous group leader will simply be replaced by "John (Group Leader)". And if there wasn't any group leader, then "John (Group Leader)" would be simply added onto the top of the list (this is not working as well).

现在,如果在检查整个列表后,没有与“John”匹配的项目,那么程序将像以前一样检查是否已经有组长。如果有,那么之前的组长将简单地替换为“约翰(组长)”。如果没有任何组长,那么“约翰(组长)”将被简单地添加到列表的顶部(这不起作用)。

Problem 1 (Scenario)

问题一(场景)

X: John

X:约翰

List:
John
Ricky
Jane

清单:
约翰·
瑞奇

OR (whatever the order of the list)

或(无论列表的顺序如何)

List:
Ricky
John
Jane

名单:
瑞奇
·约翰·

On X losing focus, item "John" should have been removed and replaced with "John (Group Leader)" added onto the top of the list, but nothing at all is happening.

在 X 失去焦点时,项目“约翰”应该被删除并替换为添加到列表顶部的“约翰(组长)”,但什么也没有发生。

Problem 2 (Scenario)

问题2(场景)

X: John

X:约翰

List:
Ricky
Jane

名单:
瑞奇
·简

On X losing focus, "John (Group Leader)" should have been added onto the top of the list, but nothing at all is happening.

在 X 失去焦点时,“John (Group Leader)”应该被添加到列表的顶部,但什么也没有发生。

And so, whenever I am having these problems, the exception shows up in my immediate window. Can anyone help me? Thank you.

因此,每当我遇到这些问题时,异常就会出现在我的即时窗口中。谁能帮我?谢谢你。

采纳答案by JDB still remembers Monica

It's difficult to diagnose all of your program's logic since you provided only a small code snippet, but as to the error you're getting, is it possible that you have a participant name which is less than 14 characters? Looking at this line:

由于您只提供了一小段代码,因此很难诊断您的所有程序逻辑,但至于您遇到的错误,您的参与者姓名是否可能少于 14 个字符?看着这一行:

If ListBox_ParticipantsNames.Items.Item(0).ToString.Substring(ListBox_ParticipantsNames.Items.Item(0).ToString.Length - 14) = "(Group Leader)" Then

You may have a chicken and egg problem where if your first participant's name does not end in "(Group Leader)" then you add it, but if it doesn't end in "(Group Leader)" then you get an error. If ListBox_ParticipantsNames.Items.Item(0).ToString.Lengthis less than 14, you'll have a negative number which will produce an index out of range exception.

您可能会遇到一个先有鸡还是先有蛋的问题,如果您的第一个参与者的名字不以“(组长)”结尾,那么您可以添加它,但如果它不以“(组长)”结尾,则会出现错误。如果ListBox_ParticipantsNames.Items.Item(0).ToString.Length小于 14,您将得到一个负数,这将产生索引超出范围异常。

See the documentation:
http://msdn.microsoft.com/en-us/library/hxthx5h6(v=vs.100).aspx

请参阅文档:http:
//msdn.microsoft.com/en-us/library/hxthx5h6(v=vs.100).aspx

ArgumentOutOfRangeException: startIndex is less than zero or greater than the length of this instance.

ArgumentOutOfRangeException: startIndex 小于零或大于此实例的长度。

It may be easier to do something like:

执行以下操作可能更容易:

Dim l_participantName As String = ListBox_ParticipantsNames.Items.Item(0).ToString()
l_participantName = l_participantName.PadLeft(14) ' Guarantees the length of the string is at least 14 characters
If l_participantName.Substring(l_participantName.Length - 14) = "(Group Leader)" Then

Even easier:

更简单:

If ListBox_ParticipantsNames.Items.Item(0).ToString.EndsWith("(Group Leader)") Then