Two similar code snippets, ONLY second gens OUT OF BOUNDS?

Var test As String = "0123456789AB"
Var test2 As String = "GFEDCBA9876543210"
Print Str(test.Length)
Print Str(test2.Length)
Print ""
For ndx As UInteger = 1 To test.Length
  Print(Str(Asc(test.Mid(ndx, 1)) - Asc(test2.Mid(ndx, 1))))
Next
If result.Length >= correctResult.Length Then
Else
 For ndx As UInteger = 1 To result.Length
  Var temp As Integer = Asc(correctResult.Mid(ndx, 1)) - Asc(result.Mid(ndx, 1))

result length = 12
correctResult length = 17

correctResult = “ليهمابتكلموشعربي؟"
result = “ష๳ၯႡႢႣႤႥႧႩႫ”

Why does ONLY the second code snippet generate an “OutOfBoundsException” at the statement “Asc(result.Mid(ndx, 1))” when ndx = 12?

1 Like

I’m looking, but please use the code tags (three backticks before and after your code) to help us help you.

1 Like

The backtick is to the left of the “1” key on US keyboards. (`)

It’s hard to say because you didn’t post your entire code, so I can’t reproduce the error. This is what I came up with:

Var test As String = "0123456789AB"
Var test2 As String = "GFEDCBA9876543210"
Print Str(test.Length)
Print Str(test2.Length)
Print ""
For ndx As UInteger = 1 To test.Length
  Print(Str(Asc(test.Mid(ndx, 1)) - Asc(test2.Mid(ndx, 1))))
Next

var correctResult as string = "ليهمابتكلموشعربي؟"
var result as string = "ష๳ၯႡႢႣႤႥႧႩႫ"

If result.Length >= correctResult.Length Then
Else
  For ndx As UInteger = 1 To result.Length
    Var temp As Integer = Asc(correctResult.Mid(ndx, 1)) - Asc(result.Mid(ndx, 1))
  Next ndx
End If

BTW, do not use UInteger (or, really, anything other than Integer) unless you have a good reason, and that usually means working directly with MemoryBlocks or system calls. Using it as a loop index can lead to unexpected, and incorrect, results in some cases.