-2
For numberOfRunsCompleted As Integer = 0 To arrayToSort.Length
    For valueWithinArray As Integer = 0 To arrayToSort.Length - 2
        If arrayToSort(arrayValue) > arrayToSort(arrayValue + 1) Then
            Dim tempStorage As Integer = arrayToSort(arrayValue)
            arrayToSort(arrayValue) = arrayToSort(arrayValue + 1)
            arrayToSort(arrayValue + 1) = tempStorage
        End If
    Next
Next

The array is declared as arrayToSort(7), being the integers from 9 to 2 inclusive.

  • sorry guys I'm new to visual basic and visual studio rn :) – Zeeshan Yahya Oct 14 '21 at 19:50
  • 1
    What is `arrayValue` and where doesn't come from? Where exactly in your code are you "outputting the array"? If I had to guess, somewhere in your code (the part that you didn't show us), your using something like `Console.WriteLine(arrayToSort)` instead of `Console.WriteLine(arrayToSort(someValue))`. – 41686d6564 stands w. Palestine Oct 14 '21 at 19:52
  • 1
    Does this answer your question? [System.Int32\[\] displaying instead of Array elements](https://stackoverflow.com/questions/18033938/system-int32-displaying-instead-of-array-elements) – GSerg Oct 14 '21 at 19:58

1 Answers1

0

I changed the name of your variable valueWithinArray to index. It really isn't the value in the array, it is the index of the element. You introduced another undeclared variable (arrayValue) where you really wanted valueWithinArray (now called index).

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim arrayToSort() As Integer = {6, 8, 7, 2, 9, 4, 5, 3}
    For numberOfRunsCompleted As Integer = 0 To arrayToSort.Length
        For index As Integer = 0 To arrayToSort.Length - 2
            If arrayToSort(index) > arrayToSort(index + 1) Then
                Dim tempStorage As Integer = arrayToSort(index)
                arrayToSort(index) = arrayToSort(index + 1)
                arrayToSort(index + 1) = tempStorage
            End If
        Next
    Next
    For Each i In arrayToSort
        Debug.Print(i.ToString)
    Next
End Sub

That was a good learning excercise but to save some typing...

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim arrayToSort() As Integer = {9, 8, 7, 6, 5, 4, 3, 2}
    Array.Sort(arrayToSort)
    For Each i In arrayToSort
        Debug.Print(i.ToString)
    Next
End Sub

The results will show up in the Immediate Window.

Mary
  • 14,926
  • 3
  • 18
  • 27
  • Thank you so much! I have only been using visual basic for 6 weeks or so so I need to learn half of this XD but I will check on microsoft docs what some of this does. Thanks! – Zeeshan Yahya Oct 16 '21 at 16:47