Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

um, a newb needs some help...

Status
Not open for further replies.

A1METALHEAD

Programmer
May 21, 2004
76
0
0
US
i am creating my first app in c#. and i have a edit box. i want to assign it to a int, how can i make shure that it dosent try to convert it if the edit box has somthing other than numbers. i am a c++ programer, and i already tryed

try
{
num = int.Parse(textBox1.Text);
}
catch (InvalidDataException)
{
return;
}

and it still comes up with an error, could you pleas help?
 
um, sorry for the post. i fixed it by using the format exeption isted of InvalidDataException. sorry
 
You also shouldn't use the Parse but the convert.
num = Convert.ToInt32(textBox1.Text);
Might as well get used to all the convert functions,
and there are lots of them. Advantage is that if you use VS then intellisense will show all the options to you.
 
thx, i dident know about Convert, why wold they have two funcions that do the same thing?
 
A1 - There are many ways to achieve the same end in programming so two functions to achuieve the same end is not uncommon. There is a requirement for both in that the integer type requires a method to Parse a given value as an integer or not. In an OO world as the operation is only to be performed on an integer the functionality belonsg within the Int32 class. Equally a class such as Convert which provides methods to perform all type conversions (well for all types from the CTL - Common Type Library) would not be complete without a method to convert another type to an integer.

marinero - Why should A1 use Convert.ToInt32 instead of Int32.Parse? Is one just a wrapper for the other ultimately making it less efficient? Or is it just a preference thing?

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top