Friday, July 31, 2009

How to transform a string into an int value in C#?

Here's my line of code:





x = int Console.ReadLine();





But the debug says I can't turn string to int, I know somethings wrong with my syntax, please help me out?





I want a user to type a number in and assign that number to "x".

How to transform a string into an int value in C#?
Hi im sorry if this is incorrect, but this is what i understand that you are trying to do.





int userInput = System.int32.Parse(Textbox.Text);


//Basically because you entered a number in a text box


//does not mean its a number and you can use it as a number


//its a string so you have to convert the string into an int


int x += userInput;





i hope that his helps Dude.
Reply:Assuming x is an integer declared as "int x", use:





x = Int32.Parse(Console.Readline());
Reply:int num = Convert.ToInt32(x);





or





int num = (int)x;





Hope this helps,





Cheers


No comments:

Post a Comment