Home Contents Search

Perpetual Newbie
Error Message Issue #23 skins QTime Perpetual Newbie Perpetual Newbie Perpetual Newbie Perpetual Newbie How Convert User Registration rabbit hole CASE & Classes skins Premium 5 Premium 6 Premium Domains Premium 2 Premium 3 Premium 4 Rare domains cities_realestate Similar   Websites education_sites entertainment_sites games misc_sites LLLL.com Site Acronym 2 Acronym 4 LLLLL.com LLLLL.com 2 LLLLL.com 3 Acronym 5 Acronym 6 Acronym 7 Acronym 8 Acronym 9 Acronym 10 Acronym 3 Brandable sites Pin Yin sites service_sites technology Acronym sites Payment Options About Our Office

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.
Remember, both StrToInt and StrToIntDef are in the SysUtils unit, so you will need to include it in your Uses clause.
 

Copyright © 2007 undu.com                    Powered by Engineer Partner The One Stop Outsource