get an integer from a text box.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there some way to convert a string which consists of a number(eg. 24) to an integer type? I am trying to get the value from the text property of a text box.
 
int n = int.Parse("24");
int n = int.Parse(TextBox1.Text);


--
Klaus H. Probst, MVP
http://www.vbbox.com/


owyn said:
Is there some way to convert a string which consists of a number(eg. 24)
to an integer type? I am trying to get the value from the text property of a
text box.
 
int n = int.Parse("24");
int n = int.Parse(TextBox1.Text);

Be sure to wrap that code in a try...catch block in case someone enters
invalid data, like this:

try
{
int n = int.Parse("24");
int n = int.Parse(TextBox1.Text);
}
catch (Exception ex)
{
// Do something to indicate TextBox1 does not contain a valid integer
}

You could also trap for the specific exceptions that Parse can throw, which
are FormatException, ArgumentException and OverflowException.

Eric
 
int num = Convert.ToInt32(textbox1.text);

dont forget to catch exception

--
Shak
(Houston)


owyn said:
Is there some way to convert a string which consists of a number(eg. 24)
to an integer type? I am trying to get the value from the text property of a
text box.
 
Hi owyn,

You can look into C# checked and unchecked keywords. It will throw exceptions if string -> int fail to work.

Correct me if i am wrong. Thanks.
 
Back
Top