I'm trying to bubble sort an array then display it in a list box. When I click the button the program crashes.

Here is my code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not TextBox1.Text = "" Then
            ListBox1.Items.Add(TextBox1.Text)
            ReDim Preserve numbers(numNumbers)
            numbers(numNumbers) = CDbl(TextBox1.Text)
            numNumbers += 1
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        BubbleSort(numbers)

        For Each x In numbers
            ListBox2.Items.Add(numbers(x).ToString())
        Next

    End Sub

And the compiler gives me this error: A first chance exception of type 'System.IndexOutOfRangeException' occurred in WindowsApplication1.exe

Recommended Answers

All 4 Replies

I'm trying to bubble sort an array then display it in a list box. When I click the button the program crashes.

Here is my code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not TextBox1.Text = "" Then
            ListBox1.Items.Add(TextBox1.Text)
            ReDim Preserve numbers(numNumbers)
            numbers(numNumbers) = CDbl(TextBox1.Text)
            numNumbers += 1
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        BubbleSort(numbers)

        For Each x In numbers
            ListBox2.Items.Add(numbers(x).ToString())
        Next

    End Sub

And the compiler gives me this error: A first chance exception of type 'System.IndexOutOfRangeException' occurred in WindowsApplication1.exe

If this helps, here is my code where I put numbers into the array:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not TextBox1.Text = "" Then
            ListBox1.Items.Add(TextBox1.Text)
            ReDim Preserve numbers(numNumbers)
            numbers(numNumbers) = CDbl(TextBox1.Text)
            numNumbers += 1
        End If
    End Sub

Edit:

I changed my Button Click code to:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        BubbleSort(numbers)

        For x = 0 To numbers.Length
            ListBox2.Items.Add(numbers(x))
        Next

    End Sub

That code now partially works, but still gives me an out of bounds exception. The Numbers appear in the list box sorted, but I still get an out of bounds exception crash.

Also if it helps, here is my sort code:

Private Sub BubbleSort(ByRef numbers() As Integer)

        Dim temp

        Dim switch = True

        While switch

            switch = False

            For x = 0 To numbers.Length - 2

                If numbers(x) > numbers(x + 1) Then

                    temp = numbers(x)

                    numbers(x) = numbers(x + 1)

                    numbers(x + 1) = temp

                    switch = True

                End If

            Next

        End While

    End Sub

    Private Sub BubbleSortDesc(ByRef numbers() As Integer)

        Dim temp

        Dim switch = True

        While switch

            switch = False

            For x = 0 To numbers.Length - 2

                If numbers(x) < numbers(x + 1) Then

                    temp = numbers(x)

                    numbers(x) = numbers(x - 1)

                    numbers(x - 1) = temp

                    switch = True

                End If

            Next

        End While

    End Sub

End Class

I'm also having trouble with the descending sort.

Try this for "out of bounds exception".

For x = 0 To numbers.Length - 1

When using .Length or .Count, it starts at 1 not 0.
Since you are looping from "0", then subtract 1 from .Length to get a even amount of loops.

As for the bubble issue, good luck.

Line 47 of your sort code should be numbers(x) = numbers(x + 1) and line 49 should be numbers(x + 1) = temp

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.