Convert string to Integer

S

Shawn Ferguson

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!
 
P

peter.tornqvist

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();
 
M

Marc Gravell

lblTotal.Text = (x + y).ToString();

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

Marc
 

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

Top