Converting string to int ???

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
 
A

Alex

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)".
 
J

James McCutcheon

int sum = Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text);

James McCutcheon
 
J

Jon Skeet [C# MVP]

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.
 
G

Ginny Caughey [MVP]

And a try...catch block is always a good idea when you're converting some
text that a user entered.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top