3

I have ran in to a problem while writing a program for school that converts a string like abc to bcd, a becomes b and b becomes c and you can see the rest.

For i = 0 To length - 1
    If (Asc(justatext.Substring(i, 1)) >= 65 And Asc(justatext.Substring(i, 1)) <= 90) Then
        Asc(justatext.Substring(i, 1) = (Asc(justatext.Substring(i, 1) + 1)))
        answer &= justatext.Substring(i, 1)
    End If
Next

This is in a function and I return the value answer, but I always get an invalid cast exception. Is there a way you can do this with ansi codes?.

  • i put words in that don't contain the letter "z" so that should not be the problem –  Apr 20 '15 at 13:09

1 Answers1

2

your problem can be found in the brackets, you have quite a lot of them, and I think you confused yourself with them.

I've tripped down your code and removed the brackets that aren't needed:

    For i = 0 To justatext.Length - 1
        If Asc(justatext.Substring(i, 1)) >= 65 And Asc(justatext.Substring(i, 1)) <= 90 Then
            answer &= Chr(Asc(justatext.Substring(i, 1)) + 1)
        End If
    Next

Watch out: this code will only work for capital letters..

Jordumus
  • 2,763
  • 2
  • 21
  • 38
  • thanks a lot! but if a but in the string "AAA" now my output in the textbox I retrun the value to is 666666 (the ansi-code itself) thanks for the reply! –  Apr 20 '15 at 13:23
  • @larsvdb97 There you go, enjoy the code! (Watch out, only works with capital letters!) – Jordumus Apr 20 '15 at 13:33
  • 2
    yeah i know, just gonna add a inf statement to make it work for lowercase letter as well ;) thank a ton! –  Apr 20 '15 at 13:35