vb.netfirst-chance-exception

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


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.

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.

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

But first, some prerequisites:

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)".

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

Problem 1 (Scenario)

X: John

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.

Problem 2 (Scenario)

X: John

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.

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


Solution

  • 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:

    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.Length is less than 14, you'll have a negative number which will produce an index out of range exception.

    See the documentation:
    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.

    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