|
|
|
| String to Integer Conversion
This article originally appeared in the February 1999 issue of Delphi Informant Magazine and is being reprinted here with permission from Informant Communications. One of the more frequently used functions in a programmers repetoire is the StrToInt function. It simply converts a string value into an integer value. Often times, programmers use this to extract a numeric value from an edit box for example. A program may ask for the number of airplanes sold during a particular month, the user enters the value in the edit box, and the programmer takes that value and converts it to an integer for internal processing. Yet StrToInt has an annoying quality. If the function cannot convert the value to a valid integer (perhaps there are non-numeric characters in the string), it generates an exception which displays �1234b is not a valid integer value�. In some cases this may be a desired effect, but often times, you don�t want to generate an exception. You could of course turn off runtime exceptions, but that can hide exceptions generated in other areas. Don�t you wish there was a function that would do this conversion and not whine about invalid data? Well, wish no longer� StrToIntDef is an extension of the StrToInt function that allows you
to specify a default value in the event that the string cannot be converted to a
valid integer value. All you do is specify the string to convert, and as a
second parameter provide the default value you want it to send back in the case
of a conversion error. The main advantage is of course that this is entirely
silent, so you can tell you had a conversion error (the default value was
returned) without disrupting the flow of the application. |
|
|
Copyright © 2007 undu.com Powered by Engineer Partner The One Stop Outsource
|