# add one to all the numbers in a textbox vb

### Question

• I asked this question a few days ago. What I wanted to do was add one to each individual number in a textbox so if the user inputted 123 then the result would 234. In order to do this a user kindly provided the following code -

```        Dim yourString As String = tb1.Text        Dim chrArray() As Char = yourString.ToCharArray()
For counter As Integer = 0 To UBound(chrArray) Step 1
If Char.IsNumber(chrArray(counter)) Then
Dim num As Integer = Asc(chrArray(counter)) + 1
chrArray(counter) = Chr(num)
End If
Next
yourString = New String(chrArray)       tb1.Text = yourString```

This worked well for all numbers except nine which returned : I assume this was because the ascii values are just being incremented and the value after 9 in ascii is : So I attempted to rectify this by instructing vb what to do if it encountered the number nine to do this I did the following -

```'Dim yourString As String = "0000200000000000"        Dim yourString As String = tb1.Text
Dim chrArray() As Char = yourString.ToCharArray()
For counter As Integer = 0 To UBound(chrArray) Step 1
If Char.IsNumber(chrArray(counter)) Then		If Asc(chrArray(counter)) = 9 Then Return 0 Else                 Dim num As Integer = Asc(chrArray(counter)) + 1
chrArray(counter) = Chr(num)
End If
Next
yourString = New String(chrArray)       tb1.Text = yourString```

This did not work so I tried the same code again with the 9 and 0 in "" however that did not work. I also tried using the ascii codes for the numbers nine and zero but this was also unsuccessful. Ultimately I want to increase the number by one unless its nine in which case I want to set it to zero. Does anyone have any advice for what I can try next?

Monday, November 04, 2013 8:24 PM

• Here's a different approach.  It doesn't use the ASCII values at all, just converts each character to a number.  If the character is non-numeric, it will display an error message in the textbox.

```        Dim yourstring As String = TextBox1.Text
Dim strdig As String = ""
Dim digit, newdig As Integer
Dim chrArray() As Char = yourString.ToCharArray()
For Each ch As Char In chrArray
If Integer.TryParse(ch, digit) Then
newdig = digit + 1
If newdig = 10 Then newdig = 0
strdig &= newdig
Else
strdig = "Non-numeric character"
Exit For
End If
Next
TextBox2.Text = strdig```

Solitaire

• Marked as answer by Monday, November 04, 2013 10:12 PM
Monday, November 04, 2013 9:52 PM

### All replies

• You could just do:

```Dim chrArray As Char() = yourString.ToCharArray()
For counter As Integer = 0 To chrArray.Length - 1
If Char.IsNumber(chrArray(counter)) Then
Dim num As Integer = Strings.Asc(chrArray(counter)) + 1
If num = 10 Then
num = 0
End If
chrArray(counter) = Strings.Chr(num)
End If
Next
yourString = New String(chrArray)

tb1.Text = yourString```

Monday, November 04, 2013 8:41 PM
• Hello,

Not sure if I got this correct to your specs so let me know if I did not. This is done in a Console app so it will work with a Windows forms app too

```Module Module1
Sub Main()
Dim Dict As New Dictionary(Of Int32, Int32) From
{
{0, 1},
{1, 2},
{2, 3},
{3, 4},
{4, 5},
{5, 6},
{6, 7},
{7, 8},
{8, 9},
{9, 0}
}

Dim UserValues As String = "61A902345B6789C"
Dim Result As String = ""

For Each c In UserValues
If Char.IsNumber(c) Then
Result &= Dict(CInt(c.ToString))
End If
Next

Console.WriteLine(Result)
End Sub
End Module
```

Result

`720134567890`

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

Monday, November 04, 2013 8:42 PM
• This did not work so I tried the same code again with the 9 and 0 in "" however that did not work. I also tried using the ascii codes for the numbers nine and zero but this was also unsuccessful.

The ASCII value for 9 is 57. See : http://en.wikipedia.org/wiki/ASCII

If you used the correct ASCII value then you need to show that code, and you need to describe the number you used to test it and the result you got from that test.   This is because your error is in the code where you do something if the value matches '9'.   You need to decide how you are going to get the right result when it matches. A common practice would be to put the value for the character before 0 back into the array, then let the next line of code increment it, so the result is '0'.  That avoids additional IF statements.  The ASCII value that comes before  '0' is 47.

Monday, November 04, 2013 8:46 PM
• This did not work so I tried the same code again with the 9 and 0 in "" however that did not work. I also tried using the ascii codes for the numbers nine and zero but this was also unsuccessful.

The ASCII value for 9 is 57

If you used the correct ASCII value then you need to show that code, and you need to describe the number you used to test it and the result you got from that test.   This is because your error is in the code where you do something if the value matches '9'.   You need to decide how you are going to get the right result when it matches. A common practice would be to put the value for the character before 0 back into the array, then let the next line of code increment it, so the result is '0'.  That avoids additional IF statements.  The ASCII value that comes before  '0' is 47.

Sorry I did not want over fill my initial post with lots of code snippets. This code is being used as an example for basic encryption. Say if a booking system stored card details. So rather than change the code each time I have just pasted the exact code I am using below -

Function encrpyt()
'this function is used to securely store the credit card numbers of customers
Dim encryptedcardnumber As String = TB_Card_Number.Text
Dim chrArray() As Char = encryptedcardnumber.ToCharArray()
For counter As Integer = 0 To UBound(chrArray) Step 1 '
If Char.IsNumber(chrArray(counter)) Then
If Asc(chrArray(counter)) = 57 Then Return 48 Else
Dim num As Integer = Asc(chrArray(counter)) + 1
chrArray(counter) = Chr(num)
End If
Next
encryptedcardnumber = New String(chrArray)
TB_Card_Number.Text = encryptedcardnumber

End Function

In order to test the function I called it on a button. I put the number 9 into a textbox and clicked the button which called the function. Usually this would change 9 to : however when using the above code nine does not change at all whilst any other number increments by one so if I tried the number 1 using the above code then two would be produced. Hope this clears things up?
Monday, November 04, 2013 9:04 PM
• Bat,

That's a very weak way to go about it, and there's no assurance you can decrypt everything you encrypt.

There are far better and more reliable ways. I'll post one such way if you're interested, but I think you're headed down a dead-end road.

...for what it's worth

Monday, November 04, 2013 9:10 PM
• This did not work so I tried the same code again with the 9 and 0 in "" however that did not work. I also tried using the ascii codes for the numbers nine and zero but this was also unsuccessful.

The ASCII value for 9 is 57

If you used the correct ASCII value then you need to show that code, and you need to describe the number you used to test it and the result you got from that test.   This is because your error is in the code where you do something if the value matches '9'.   You need to decide how you are going to get the right result when it matches. A common practice would be to put the value for the character before 0 back into the array, then let the next line of code increment it, so the result is '0'.  That avoids additional IF statements.  The ASCII value that comes before  '0' is 47.

Sorry I did not want over fill my initial post with lots of code snippets. This code is being used as an example for basic encryption. Say if a booking system stored card details. So rather than change the code each time I have just pasted the exact code I am using below -

Function encrpyt()
'this function is used to securely store the credit card numbers of customers
Dim encryptedcardnumber As String = TB_Card_Number.Text
Dim chrArray() As Char = encryptedcardnumber.ToCharArray()
For counter As Integer = 0 To UBound(chrArray) Step 1 '
If Char.IsNumber(chrArray(counter)) Then
If Asc(chrArray(counter)) = 57 Then Return 48 Else
Dim num As Integer = Asc(chrArray(counter)) + 1
chrArray(counter) = Chr(num)
End If
Next
encryptedcardnumber = New String(chrArray)
TB_Card_Number.Text = encryptedcardnumber

End Function

In order to test the function I called it on a button. I put the number 9 into a textbox and clicked the button which called the function. Usually this would change 9 to : however when using the above code nine does not change at all whilst any other number increments by one so if I tried the number 1 using the above code then two would be produced. Hope this clears things up?

The more details provided in a question allows others to understand what you want and not to waste others time in perhaps a wrong direction.

Also when posting code use the code button as shown below

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

Monday, November 04, 2013 9:22 PM
• If Asc(chrArray(counter)) = 57 Then Return 48 Else

This is incorrect because you are not allowing for the remainder of the code to execute and undo whatever that line is doing. If you want to calculate 48 as the result then you need to either (a) do what the next line of code does instead, and prevent that next line from executing or (b) (my suggestion) set things up so that when the next line of code executes it gives the result you want ('0').

This is a difficult and inefficient way to do what you need.  I would recommend starting over with something that is designed to do the job you want.  It will be bother simpler and more effective.

• Edited by Monday, November 04, 2013 9:38 PM frmt
Monday, November 04, 2013 9:35 PM
• Here's a different approach.  It doesn't use the ASCII values at all, just converts each character to a number.  If the character is non-numeric, it will display an error message in the textbox.

```        Dim yourstring As String = TextBox1.Text
Dim strdig As String = ""
Dim digit, newdig As Integer
Dim chrArray() As Char = yourString.ToCharArray()
For Each ch As Char In chrArray
If Integer.TryParse(ch, digit) Then
newdig = digit + 1
If newdig = 10 Then newdig = 0
strdig &= newdig
Else
strdig = "Non-numeric character"
Exit For
End If
Next
TextBox2.Text = strdig```

Solitaire

• Marked as answer by Monday, November 04, 2013 10:12 PM
Monday, November 04, 2013 9:52 PM
• Here's a different approach.  It doesn't use the ASCII values at all, just converts each character to a number.  If the character is non-numeric, it will display an error message in the textbox.

```        Dim yourstring As String = TextBox1.Text
Dim strdig As String = ""
Dim digit, newdig As Integer
Dim chrArray() As Char = yourString.ToCharArray()
For Each ch As Char In chrArray
If Integer.TryParse(ch, digit) Then
newdig = digit + 1
If newdig = 10 Then newdig = 0
strdig &= newdig
Else
strdig = "Non-numeric character"
Exit For
End If
Next
TextBox2.Text = strdig```

Solitaire

This solution worked. Thanks. To be honest though I don't full understand how the code works would you be able to comment what is happening?
Monday, November 04, 2013 10:13 PM
• ```        Dim strdig As String = ""
Dim digit As Integer

For Each ch As Char In TextBox1.Text 'examine each char in textbox
If Integer.TryParse(ch, digit) Then 'if char is number store it in digit
digit += 1 'add 1 to it
If digit = 10 Then digit = 0 '10 becomes 0
strdig &= digit.ToString 'convert number to string and concatenate
Else
'char was not a number
strdig = "Non-numeric character" 'error message
Exit For 'exit loop
End If
Next

TextBox2.Text = strdig
```

• Edited by Monday, November 04, 2013 11:41 PM
Monday, November 04, 2013 11:37 PM