Convert string to Integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shawn Ferguson

    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(t xtBox1.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!

  • peter.tornqvist@gmail.com

    #2
    Re: Convert string to Integer

    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


    Comment

    • Marc Gravell

      #3
      Re: Convert string to Integer

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

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

      Marc


      Comment

      • Mr Icekirby

        #4
        RE: Convert string to Integer

        int.Parse(TextB ox1.Text) is a little easier to understand and quicker to type.

        Comment

        Working...