By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,505 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,505 IT Pros & Developers. It's quick & easy.

Convert string to Integer

P: n/a
Hello All,

I'm trying to do what I would think would be simple and straightforward, but it is not. I have a 2 textbox on a form, a label, and a button, when I click the botton I want to add the 2 textboxes and place the value in the label. Unfortunately, when I try to convert.Int32(txtBox1.Text) I get cannot convert string to int error. Basically, Im doing this:

int x;
int y;
x = (int) Convert.ToInt32(TextBox1.Text);
y = (int) Convert.ToInt32(TextBox2.Text);

lblTotal = x + y;
Error Message:
Error 1 Cannot implicitly convert type 'int' to 'string'
Can you help me understand why this does not work and what I can do to make it work. Thanks you!

Nov 7 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
It is not the convert call that is causing the problem. It is this:

lblTotal = x + y;

You should do it like this:

int x = Convert.ToInt32("23");
int y = Convert.ToInt32("43");

int w = x + y;

string t = w.ToString();

--
Regards, Peter
Nov 7 '06 #2

P: n/a
lblTotal.Text = (x + y).ToString();

also, you don't need the (int) casts, since ToInt32() returns int

Marc
Nov 7 '06 #3

P: n/a
int.Parse(TextBox1.Text) is a little easier to understand and quicker to type.
Nov 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.