title: Dynamically Resizing Arrays
body: <p>I ran into the following issue when dealing with adding to variable array sizes. The loop runs one time more then it should essentially. But I'm curious as to what would cause this behavior? Am I not quite understanding how the loop exit functions are being called?</p>
  
  <p>Here's the code: </p>
  
  <pre><code>Module Module1
  
  Sub Main()
      Dim num(-1)
      Dim i = 0
      Console.WriteLine("Input numbers to be added, q to stop:")
      Dim input
      Dim total = 0
      Do
          ReDim Preserve num(UBound(num) + 1) : num(UBound(num)) = i 'resize the array each time before an element is added.
          input = Console.ReadLine
          If IsNumeric(input) Then 'attempt to break loop on non numeric input
              num(i) = CInt(input)
              i += 1
          Else
              Exit Do
          End If
      Loop
      total = 0
      For Each n In num
          total += n
      Next
      Console.WriteLine(Join(num))
      Console.WriteLine("Total: " &amp; total)
  
      Console.ReadLine()
  </code></pre>
  
  <p>For input: <code>1 2 3 4 5 q</code>, the output I get is:</p>
  
  <pre>1 2 3 4 5 5
  Total: 20</pre>
  
  <p>It adds the last element twice, which is interesting as it is not only running twice but somehow using the last numeric input even though the final input was not numeric. Does anyone know why that would be?</p>