Thursday, July 29, 2010

Visual C # Express - Parsing user input (4th in Serie A) Visual Basic


Image : http://www.flickr.com

In my first three articles about Visual C # Express, I have written enough in general terms how to build modern C # programs, using the example program InterestCalculator. Visual Basic In this article I talk about the very specific problem to convert user input text information from numerical information.
TextBox only accept strings
There are four user input boxes in the form of text CompoundGrowth InterestCalculator program, and allSeem to accept numbers but in reality it is not. Visual Basic We accept all the strings, for one simple reason. Strings are the only data. Net TextBox control. In fact the only way to read the contents of the TextBox control is via the property "Text, which returns a string.
Parse Function rescue
Since the TextBox control only accepts strings, the programmer is stuck with the problem of conversion from a C # string to a numeric type C #. Visual Basic There are in factseveral ways to do this using the. network library class, but this article will show you how to do it using the analysis function.
Almost every numeric data type has a Parse function that operates on a string and converts it into a kind of digital data. For example, the code to convert a string to a double would look like this:
/ / Converts a string txtPrincipal TextBox to a double precision number
mPrincipal Double.Parse = (txtPrincipal.Text);

That's all there is to it. . . Visual Basic except for one small problem. What happens when someone types something like "1A3Y" txtPrincipal in TextBox? Now we have a problem.
Recovering from incorrect input
It would be easy to design forms if people always hit the right buttons and always entered the correct data. The challenge of designing and encoding forms is to give them reasonable error recovery scenarios.
L '. NET librarysupports a solution to detection and management of incorrect input. In addition to the Parse function is a function TryParse associated with each type of numeric data. Visual Basic TryParse function does exactly what you might think, try to parse a string and returns true if it can and Boolean false if it is not possible.
Now the code, as contained in ValidateInput function more like this:
if (! double.TryParse (txtPrincipal.Text outmPrincipal))
(
txtAmount.AppendText ("the principal must be a number");
success = false;
)
The same test is repeated for each type of text boxes in numerical form, function and ValidateInput false if any test fails.
In addition, as shown above (or copies of downloaded code) an error message is printed to the user text box Each Which does not validate correctly,Visual Basic  and the value of investment made will not be displayedunless all four input text box validation.
If an error message is displayed, the user can correct the contents of the TextBox error (s) and resubmit pressing the "Calculate" amount again.
Other options
Although this article does not discuss them, Visual Basic let me briefly mention two other options to convert numeric string.
(1) Building conversion TextBox, inheriting the TextBox class and adding aconverting text-to-digital or method. This is a very elegant solution and would make much sense if you were using a lot of text boxes for numeric input.
(2) The Convert class contains methods to convert every base type to every other base type. Visual Basic Methods Convert.ToDouble Convert.ToUInt64 and could be used in place of double.TryParse and ulong.TryParse respectively, but these methods do not return the status of the conversion so that the programmer would need to implementexception code to intercept invalid input.
Summary
You can find a copy of the Code, Visual Basic interest on my article Support Page.
L '. Net TextBox class allows only text input, so that the programmer needs a way to convert the string value of text box to a numeric value. TryParse function solves this problem with the added bonus that performs error checking and returns the boolean state of conversion.
batteries plus store pc game racing games reviews

No comments:

Post a Comment