Converting string to int ???

  • Thread starter Thread starter tiger79
  • Start date Start date
T

tiger79

Hi,
i'm reading some numeric values from a couple of textboxes...
I'd like to sum these values :

int sum = (int)textBox1.Text + (int)textBox2.text

this wont work cause it says that it cant convert string to int...
How do I mangage to accomplish a sum between two (or more) values read from
textboxes ???
Thanx
 
Hi,

use Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)

or

int.Parse(textBox1.Text)...

C# is strongly typed language and you cannot explicitly convert string to
int by "(int)".
 
int sum = Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text);

James McCutcheon
 
tiger79 said:
i'm reading some numeric values from a couple of textboxes...
I'd like to sum these values :

int sum = (int)textBox1.Text + (int)textBox2.text

this wont work cause it says that it cant convert string to int...
How do I mangage to accomplish a sum between two (or more) values read from
textboxes ???

Use Int32.Parse to convert from an integer to a string - or
Convert.ToInt32.
 
And a try...catch block is always a good idea when you're converting some
text that a user entered.
 
Back
Top