11 Replies - 1872 Views - Last Post: 30 May 2009 - 07:59 AM Rate Topic: -----

#1 C++++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-November 07

convert string to int

Posted 29 May 2009 - 10:23 PM

Hi every one

I try to make program to add 7 number in C# window application

so I make 8 text box and one button for calculate

code :
  private void button2_Click(object sender, EventArgs e)
		{
			string num1, num2, num3, num4, num5, num6, num7;
			num1 = TextBox1.Text;
			num2 = TextBox2.Text;
			num3 = TextBox3.Text;
			num4 = TextBox4.Text;
			num5 = TextBox5.Text;
			num6 = TextBox6.Text;
			num7 = TextBox7.Text;

			Convert.ToInt32(num1);
			Convert.ToInt32(num2);
			Convert.ToInt32(num3);
			Convert.ToInt32(num4);
			Convert.ToInt32(num5);
			Convert.ToInt32(num6);
			Convert.ToInt32(num7);

			TextBox8.Text = num1 + num2 + num3 + num4 + num5 + num6 + num7;
		}  


but the problem is when I click the button the answer is = num1 num2 num3 num4 num5 num6 num7
which means no add happened.

Can any one explain why?

Is This A Good Question/Topic? 0
  • +

Replies To: convert string to int

#2 Ändrew  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 312
  • Joined: 21-April 08

Re: convert string to int

Posted 29 May 2009 - 10:39 PM

OLD:

Quote

You should get rid of all those convertions and just use

int num1, num2, num3, num4, num5, num6, num7;



Reason why its doing that is becasue I think its still plusing the strings.

Ändrew

This post has been edited by Ändrew: 29 May 2009 - 10:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 C++++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-November 07

Re: convert string to int

Posted 29 May 2009 - 10:42 PM

thnks Ändrew for your replay

but I use windowApplication not console

This post has been edited by C++++: 29 May 2009 - 10:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 Ändrew  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 312
  • Joined: 21-April 08

Re: convert string to int

Posted 29 May 2009 - 10:44 PM

You might be confused,

int , string , bool are all varibles and can be used in all cases dosn't matter if its on a console or a form they are all the same.

int = numbers (Default int32)
string = "all crazy stuff in quotes"
bool = true or false

This post has been edited by Ändrew: 29 May 2009 - 10:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 C++++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-November 07

Re: convert string to int

Posted 29 May 2009 - 10:48 PM

ya I know all are a variables

but when you run aprogram and write number in textbox it's consider string
Was This Post Helpful? 0
  • +
  • -

#6 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: convert string to int

Posted 29 May 2009 - 10:57 PM

try this:
int num1, num2, num3, num4, num5, num6, num7;
			num1 = Convert.ToInt32(textBox1.Text);
			num2 = Convert.ToInt32(textBox2.Text);
			num3 = Convert.ToInt32(textBox3.Text);
			num4 = Convert.ToInt32(textBox4.Text);
			num5 = Convert.ToInt32(textBox5.Text);
			num6 = Convert.ToInt32(textBox6.Text);
			num7 = Convert.ToInt32(textBox7.Text);

			textBox8.Text = (num1 + num2 + num3 + num4 + num5 + num6 + num7).ToString();

Was This Post Helpful? 1
  • +
  • -

#7 Ändrew  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 312
  • Joined: 21-April 08

Re: convert string to int

Posted 29 May 2009 - 10:58 PM

I think its because the varible is still a string when it comes out the other end.
Was This Post Helpful? 0
  • +
  • -

#8 C++++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-November 07

Re: convert string to int

Posted 29 May 2009 - 11:09 PM

Mr. Noorahmed
it works thank you

Mr.Andrew thank you too
Was This Post Helpful? 0
  • +
  • -

#9 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: convert string to int

Posted 29 May 2009 - 11:12 PM

View PostC++++, on 30 May, 2009 - 09:39 AM, said:

Mr. Noorahmed
it works thank you

Mr.Andrew thank you too


you welcome
Was This Post Helpful? 0
  • +
  • -

#10 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: convert string to int

Posted 30 May 2009 - 01:45 AM

Although Convert.ToInt32() will work it is wiser to use the int.TryParse() and int.Parse() pair because:
  • It's more OO friendly. Using a separate class to convert another object to a different object (i.e. using Convert.ToInt32() etc.) makes no sense in OO Paradigm. It's more sensible to use a conversion function from one the classes that is being converted.
  • The TryParse() function can tell you if the string can be converted to an integer or not in the first place. This helps in writing sturdy text validation routines.

Was This Post Helpful? 0
  • +
  • -

#11 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: convert string to int

Posted 30 May 2009 - 05:13 AM

Your error is that you are adding strings

look this line " num1 + num2 "

"num1" and "num2" both are string and the result will be a added string which you are getting

Example

Let i have three textbox name "textbox1" ,"textbox2" and "textbox3"


private void button2_Click(object sender, EventArgs e)
		{
		  
		   int num1 =  Convert.ToInt32(textBox1.Text);
		   
		   int num2 =  Convert.ToInt32(textBox2.Text);

		   int result = num1 + num2;
		  
		   textbox3.Text = result.ToString();

	   }




In the first step i have converted the textbox1 text to integer and put the value in num1

Second step i have converted the textbox2 text to integer and put the value in num2

Third i have added num1 and num2 ,Note i have added integers not strings
and put it in the variable result

Fourth i converted the integer value to string using ToString() method to show the result in textbox3

You can get good lessons over C# on microsoft website check it out

hope it helped.
Was This Post Helpful? 0
  • +
  • -

#12 C++++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-November 07

Re: convert string to int

Posted 30 May 2009 - 07:59 AM

thanks to all u guys
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1