Home > Net >  "Index was outside the bounds of the array" problem with my sort program
"Index was outside the bounds of the array" problem with my sort program

Time:02-01

I have got a program using the vb.net framework and it seems to be getting an error. Its simply just a BubbleSort algorithm that I have used before yet for some reason it isn't working. To start the program the user places in a txt file that presents itself in a listbox, this part works fine. Entering the next form is where the information from the textbox is sorted using the BubbleSort, after a button click the listbox will clear and paste the new sorted list, into the same listbox. That's when the error appears.

Here's the code:

Private Sub btnNameSort_Click(sender As Object, e As EventArgs) Handles btnNameSort.Click

    lstSortArray.Items.Clear()

    Dim Data() As String = DataEntry.lstPatientArray.Items.Cast(Of Object).Select(Function(o) DataEntry.lstPatientArray.GetItemText(o)).ToArray

    Count = DataEntry.lstPatientArray.Items.Count

    Dim Swapped As Boolean
    Dim comparisonNo As Integer
    Dim temp As String
    Dim i As Integer

    Swapped = True

    While Swapped = True
        Swapped = False
        comparisonNo = 1
        While comparisonNo < Count
            If Data(comparisonNo) > Data(comparisonNo   1) Then
                temp = Data(comparisonNo)
                Data(comparisonNo) = Data(comparisonNo   1)
                Data(comparisonNo   1) = temp
                Swapped = True
            End If
            comparisonNo = comparisonNo   1
        End While
    End While

    For i = 1 To Count
        lstSortArray.Items.Add(Data(Count))
    Next

End Sub

The Line at which the error appears: If Data(comparisonNo) > Data(comparisonNo 1) Then

DataEntry.lstPatientArray is the listbox on the other form if you need to know, and Count has been declared its just at the start of the form as an integer.

I am new to coding so any help will be appreciated. Thanks!

CodePudding user response:

Arrays in .net are zero based. The index of the first element of the array is 0. When an array is initialized in code, as you have done here, the index of the last element will be one less than the Count. Remember we started at zero.

We don't need i because we are using a For Each loop to display the result.

Inside the first loop, you don't want to set comparisonNo = 1. The array indexes start at zero. To keep the indexes in range we need to subtract 2 from Length. The indexes end one less than the Length and when we compare to i 1 we need to subtract another 1.

**EDIT**

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim a() = {"Monty Reyes", "Kier Burke", "Kallum Wiley", "Raja Krueger", "Clarissa Webb", "Connah Mathews", "Alisa Bassett", "Emmeline Mills"}
    Dim Swapvalue As String
    For ii = 0 To a.Length - 2
        For i = 0 To a.Length - 2
            If a(i) > a(i   1) Then
                Swapvalue = a(i)
                a(i) = a(i   1)
                a(i   1) = Swapvalue
            End If
        Next
    Next

    For Each st In a
        Debug.Print(st)
    Next
End Sub

Of course you can always do...

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim a() = {"Monty Reyes", "Kier Burke", "Kallum Wiley", "Raja Krueger", "Clarissa Webb", "Connah Mathews", "Alisa Bassett", "Emmeline Mills"}
    Array.Sort(a)
    For Each st In a
        Debug.Print(st)
    Next
End Sub
  •  Tags:  
  • Related