Home Contact Us Site Map
Monday, December 11, 2017
Article written by Samuel Mela

Visual C# Express - Parsing User Input (4th In A Series)

In my first three articles about Visual C# Express, I wrote in fairly general terms about how to build modern C# programs, using the example of the InterestCalculator program. In this article I would like to talk about the very specific problem of converting user input from textual information to numeric information.

TextBox Accepts only Strings

There are four user input text boxes in the CompoundGrowth form of the InterestCalculator program, and all of them APPEAR to accept numeric; but in fact they do not. They all accept strings, for one simple reason. Strings are the only data accepted by the .net TextBox control. In fact the only way to read the contents of the TextBox control is via the "Text" property, which returns a string.

Parse Function to the Rescue

Since the Textbox control accepts only strings, the programmer is stuck with the problem of converting from a C# string type to a C# numeric type. There are in fact several methods for doing this using the .net class library, but in this article I will show you how to do it using the Parse function.

Nearly every numeric data type has a Parse function which operates on a string and converts it to a numeric data type. For example, the code to convert from a string to a double would look something like the following:



// Convert string in txtPrincipal TextBox to a double precision number mPrincipal= Double.Parse( txtPrincipal.Text);


That's all there is to it . . . except for one small problem. What happens when if someone types something like "1A3Y" into the txtPrincipal TextBox? Now we have a problem.

Recovering from Erroneous Inputs

It would easy to design forms if people always hit the right buttons and always entered the correct data. The challenge of designing and coding forms is to give them reasonable error recovery scenarios.

The .net class library supports a solution to the problem detecting and handling erroneous input. In addition to the Parse function there is a TryParse function associated with each numeric data type. The TryParse function does just what you would think, it tries to parse a text string and returns boolean true if it is able to and boolean false if it cannot.

Now the code, contained in the function ValidateInput looks more like this:

if (!double.TryParse(txtPrincipal.Text, out mPrincipal))
{
txtAmount.AppendText("Principal must be a number");
success = false;
}

The same type test is repeated for each of the numeric text boxes in the form, and the function ValidateInput returns false if any of the tests fail.

In addition, as you can see above (or in your copy of downloaded code), an error message is printed to the user for each text box which does not validate properly; and the compounded investment value will not be displayed unless all four text box inputs validate.

If an error message is displayed, the user has the option of correcting the contents of the invalid TextBox(es) and resubmitting by pushing the "Calculate Amount" button again.

Other Options

Although this article did not discuss them, I would like to briefly mention two other options for doing the string to numeric conversion.

(1) Build the conversion into the TextBox, by inheriting the TextBox class and adding a text-to-numeric conversion property or method. That is a very elegant solution and would make a lot of sense if you were using a lot of numeric input text boxes.

(2) The Convert class contains methods to convert every base type to every other base type. The methods Convert.ToDouble and Convert.ToUInt64 could be used instead of double.TryParse and ulong.TryParse respectively; however these methods do not return the status of the conversion so the programmer would need to implement exception code to trap invalid input.

Summary

You can find a copy of the Interest Calculator Code on my Article Support Page.

The .net TextBox class allows only textual input, so the programmer needs a way to convert the string value of the TextBox to a numeric value. The TryParse function solves this problem with the additional bonus that it performs error checking and returns the boolean status of the conversion.
Related Searches

data type