471,594 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Convert string to Integer

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
3 9732
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
lblTotal.Text = (x + y).ToString();

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

Marc
Nov 7 '06 #3
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.

Similar topics

5 posts views Thread by IamZadok | last post: by
3 posts views Thread by Convert TextBox.Text to Int32 Problem | last post: by
8 posts views Thread by Carlos | last post: by
5 posts views Thread by Mika M | last post: by
14 posts views Thread by Drew | last post: by
20 posts views Thread by Niyazi | last post: by
3 posts views Thread by priyanka | last post: by
7 posts views Thread by elliotng.ee | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.