Jump to content
  • Advertisement
Sign in to follow this  
xyuri

Delphi type checking?

This topic is 4745 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm doing an assignment and as you can guess its due pretty soon and dont have some to ask the lecturer how to do this AGAIN ... I asked him last lesson and no matter how much I argued that the SrtToInt would completely crash the app is the string was not numeric, he reconed it would work?! Could someone please help me out with just this? I need to check if a string is all numeric or not. Any help is appreciated. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
StrToInt will raise an exception (which you can catch) if it's not a valid integer. However using exceptions for basic input checking is not good programming practice [from a code or performance standpoint] (only Java resorts to this). StrToInt (with it's exceptions) should really only be used when you are reading from some pre-processed source, where a non-numeric input is not just a user mistake, but represents actual failure/curruption somewhere.

Alternatives include:

SysUtils:
function TryStrToInt(const S: string; out Value: Integer): Boolean;

And of course the base Pascal (predates Delphi and Object Pascal) conversion function, on which the other two functions are actually built:

procedure Val(S; var V; var Code: Integer);

Which takes a string and a numeric variable (integer, float, etc) and attempts to convert it. If Code=0 then conversion was successful to the type of the variable passed. Otherwise Code has the index in the string where the problem was encountered.

Share this post


Link to post
Share on other sites
Woa, you know what? I really dont like Delphi, but a, beginning to like it because of the kind of inbuild functions it has :-) If only the IDE was as easy to use as VB.NET ! :D

Thank you very much Michalson, I appreciate the help :-)

I really hope when the lecturer was telling me to "Try StrToInt" he wasnt meaning "TryStrToInt" :D

Share this post


Link to post
Share on other sites

Hmm..

I always found the Delphi IDE to be rather intuitive. It's laid out just as well if not better than other RAD software.

Later,

GCS584

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!