473,324 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Numbers in Strings

Hi. I'm trying to create a program where a user can type in a 5 digit number and my program has to add the first and third digit numbers together. I'm having difficulty getting the program to work. I'm using Visual Basic 2005. Any advice?
Oct 27 '07 #1
4 1120
cugone
20
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:
Expand|Select|Wrap|Line Numbers
  1. dim intFirst as Short = Convert.toInt16(txtNumber.Text.Chars(0))
  2. dim intSecond as Short = Convert.toInt16(txtNumber.Text.Chars(2))
  3. dim intAnswer as Short = intFirst + intSecond
  4. MessageBox.Show(intAnswer.ToString)
Line by Line:

Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. MessageBox.Show(intAnswer.ToString)
The answer is converted to a string for display into a dialog popup.
Oct 28 '07 #2
Thanks, that helps. When I put in the number "12345" into the textbox, it's giving me the answer 102. Any ideas on why it's giving me that answer?



Every string object has a method ...
Oct 28 '07 #3
QVeen72
1,445 Expert 1GB
Hi,

Use "SubString" :

Expand|Select|Wrap|Line Numbers
  1. Dim intFirst As Short = Convert.ToInt16(TextBox1.Text.Substring(0, 1))
  2. Dim intSecond As Short = Convert.ToInt16(TextBox1.Text.Substring(2, 1))
  3. Dim intAnswer As Short = intFirst + intSecond
  4. MessageBox.Show(intAnswer.ToString)
  5.  
  6.  
This adds First Digit and Third Digit..

Regards
Veena
Oct 28 '07 #4
That worked perfectly!! Thanks, you are a life-saver!!

Use "SubString" : ...
Oct 28 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Eduardo Bezerra | last post by:
Hi, I'm looking for an efficient way to create the oposite of a list of numbers. For example, suppose I have this list of numbers: 100 200 300
17
by: Steve Jorgensen | last post by:
If you've ever employed custom error numbers and messages in you programs, you've probably ended up with code similar to what I've ended up with in the past something like... <code> public...
89
by: purifier | last post by:
The problem is to write a program in 'C' to find the greatest of 2 given numbers... Easy? huh here's the catch do not use 'if' or any conditional statements if u want it to be a little more...
3
by: Mars | last post by:
I don't know whether the inputs are all numbers or all strings... if they are numbers, they are sorted as numbers, if they are strings, they are sorted as strings.... e.g. input : 9 12 2...
4
by: Sam Kong | last post by:
Hello! I wonder if there's a good way to make a function that calculates 2 big numbers (whole numbers) expressed as strings and return a result string with the value of a number? For example:...
28
by: MLH | last post by:
The largest integer A97 can deal with is 2,147,483,647, as I understand it from HELP. I would be content to represent larger integers as strings. For example, "2147483648" would suit me fine. I...
11
by: balakrishnan.dinesh | last post by:
hi frnds, Im having two 20digit numbers, But while comparing those it is giiving wrong ouput in javascript. for example here is my code, my secanrio is , ~ If first 20 digit number is...
10
by: Dave Cox | last post by:
of "int" in VB? //b=1.2 b=int(b) //b now equals 1
15
by: Szabolcs | last post by:
Newbie question: Why is 1 == True and 2 == True (even though 1 != 2), but 'x' != True (even though if 'x': works)?
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.