title: startIndex cannot be larger than length of string. Parameter name: startIndex in vb.net
body: <p>I have a piece of code that generates random characters. The problem is, every once in a while, it returns an error: <br>
  <strong>"startIndex cannot be larger than length of string.
  Parameter name: startIndex"</strong></p>
  
  <p>How do I prevent this kind of error from happening?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>Friend Function gentCtrlChar()
      Dim ran As New Random
      Dim alpha As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      Dim alpha2 As String = "ZYXWVUTSRQPONMLKJIHGFEDCBA"
      Dim rdm As New Random
      Dim genChar As String = ""
      For i As Integer = 1 To 52
          Dim selChar As Integer = rdm.Next(1, 28)
          Dim selChar2 As Integer = rdm.Next(1, 28)
          genChar = genChar + "" + alpha.Substring(selChar, 1) + "" + alpha2.Substring(selChar2, 1)
          On Error Resume Next
          Exit For
      Next
      Return genChar
  End Function
  </code></pre>
  
  <p>as you can see, I tried putting the "On Error Resume Next" hoping that somehow, this will take care of the error for me. But sadly, It doesn't do it's job. Or am I using it the wrong way or for the wrong situation?</p>
  
  <p>Any help?</p>
  
  <p>Thanks!</p>