I am trying to sort my array in ascending&Descending order, BUT im having trouble because the code to enter numbers and the code to sort the array was given to me by my professor but i changed the code to enter numbers into the array, so i think that's what messing my code to sort arrays upbecause the codes aren't matching up....any help is appreciated
Code:Option Strict On Public Class Form1 Dim J As Integer = 0 Dim MyArray As New List(Of Integer) Private Sub btnPutNumberIntoArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPutNumberIntoArray.Click Dim number As Integer Integer.TryParse(txtNumbers.Text, number) If number < 1 Then MessageBox.Show("Please enter a number greater then zero.") Else MyArray.Add(number) txtNumbers.Clear() End If End Sub Private Sub btnDisplayNumbersInArray_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayNumbersInArray.Click txtDisplayNumbers.Clear() txtDisplayNumbers.Focus() For J As Integer = 0 To MyArray.Count - 1 txtDisplayNumbers.Text &= MyArray(J).ToString & Environment.NewLine Next End Sub Private Sub btnDisplayAscendingOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayAscendingOrder.Click Dim SwapFlag As Boolean Dim UpperSub As Integer Dim Temp As Integer UpperSub = 24 SwapFlag = True While SwapFlag = True And UpperSub >= 1 J = 0 SwapFlag = False While J <= UpperSub - 1 If MyArray(J) > MyArray(J + 1) Then Temp = MyArray(J) MyArray(J) = MyArray(J + 1) MyArray(J + 1) = Temp SwapFlag = True End If J = J + 1 End While UpperSub = UpperSub - 1 End While txtDisplayNumbers.Text = txtDisplayNumbers.Text & CStr(MyArray(J)) & ControlChars.NewLine End Sub Private Sub btnDisplayDescendingOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayDescendingOrder.Click Dim SwapFlag As Boolean Dim UpperSub As Integer Dim Temp As Integer UpperSub = 24 SwapFlag = True While SwapFlag = True And UpperSub <= 1 J = 0 SwapFlag = False While J >= UpperSub - 1 If MyArray(J) < MyArray(J + 1) Then Temp = MyArray(J) MyArray(J) = MyArray(J + 1) MyArray(J + 1) = Temp SwapFlag = True End If J = J + 1 End While UpperSub = UpperSub - 1 txtDisplayNumbers.Text = txtDisplayNumbers.Text & CStr(MyArray(J)) & ControlChars.NewLine End While End Sub
At the red Highlight i get this error:
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"




because the codes aren't matching up....any help is appreciated 
Reply With Quote