Using TryParse()
You are correct in that all data entered into a textbox is string data. As such, it cannot be manipulated directly as numeric data. You must use a conversion method to change the data in the textbox to a numeric.
There are alternatives. For example, you could use int.Parse() to perform the conversion of the text data to an integer. That the good news. The bad news is when the user enters non-numeric data, the code could throw an exception and crash your program. (Exceptions are covered in Chatper 11.) The TryParse() method, however, prevents most exceptions and is safer than the simple Parse() method. (That's why I introduced it in Chapter 6, because I didn't want to get into exception handling until later.)
I hope this answers your question.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
|