Every string object has a method called "Chars()" that allows you to pull out characters at specific locations. In your case, you're using a textbox, so it will be [Object_Name].Text.Chars()
For example:
- dim intFirst as Short = Convert.toInt16(txtNumber.Text.Chars(0))
-
dim intSecond as Short = Convert.toInt16(txtNumber.Text.Chars(2))
-
dim intAnswer as Short = intFirst + intSecond
-
MessageBox.Show(intAnswer.ToString)
Line by Line:
- dim intFirst as Short = Convert.toInt16(txtNumber.Text.Chars(0))
1. The Chars() method extracts the first character (zero-based indexing).
2. Converts it to a 16-bit integer (0 to 65,534), because converting a single digit to anything bigger is a waste of memory.
3. The result is then stored into a 16-bit signed integer.
Note: You could also use Convert.ToByte (8-bit unsigned integer) and store in Byte data types to save even more memory because the numbers extracted can't be less than 0 and are obviously less than 255.
- dim intSecond as Integer = Convert.toInt16(txtNumber.Text.Chars(2))
1. The Chars() method extracts the third character (zero-based indexing).
2. Converts it to a 16-bit integer (0 to 65,534), because converting a single digit to anything bigger is a waste of memory.
3. The result is then stored into a 16-bit signed integer.
Note: You could also use Convert.ToByte (an 8-bit unsigned integer) and store in Byte data types to save even more memory because the numbers extracted can't be less than 0 and are obviously less than 255.
- dim intAnswer as Integer = intFirst + intSecond
1. The first number and the second number variables are added together
2. The result is stored in a 16-bit signed integer
Note: As above, you can also use 8-bit unsigned integers.
- MessageBox.Show(intAnswer.ToString)
The answer is converted to a string for display into a dialog popup.