title: Bubble sort index was outside the bounds of the array
body: <pre><code>Public Class Form3
  Public Swapped As Boolean
  Public ComparisonNumber As Integer
  Dim Temp As String
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
      Swapped = True
      While Swapped = True
          Swapped = False
          ComparisonNumber = 0
          While ComparisonNumber &lt; PatientCount
              If Names(ComparisonNumber) &gt; Names(ComparisonNumber + 1) Then
                  Temp = Names(ComparisonNumber)
                  Names(ComparisonNumber) = Names(ComparisonNumber + 1)
                  Names(ComparisonNumber + 1) = Temp
                  Swapped = True
              End If
              ComparisonNumber = ComparisonNumber + 1
          End While
      End While
  
      For i = 0 To PatientCount
          lstPatientNames.Items.Add(Names(i))
          lstPatientHeights.Items.Add(Heights(i))
          lstPatientWeights.Items.Add(Weights(i))
      Next
  
  End Sub
  End Class
  </code></pre>
  
  <p>This is the code for my bubble sort for a project but when I press the button that sorts and displays the array I get the error: <strong><code>"index was outside the bounds of the array"</code></strong> on the line</p>
  
  <pre><code>If Names(ComparisonNumber) &gt; Names(ComparisonNumber + 1) Then
  </code></pre>
  
  <p>Any ideas as to why?
  Thanks</p>