Polar Bear Posted August 5, 2005 Posted August 5, 2005 Hi, Is there a more efficient way to check if an object can be converted to a number than this: Function IsNumber(input As Object) As Boolean Dim out As Double Return Double.TryParse(input, System.Globalization.NumberStyles.Number, System.Globalization.NumberFormatInfo.CurrentInfo, out) End Function Quote
bri189a Posted August 5, 2005 Posted August 5, 2005 Well you can always try converting the number in a try/catch. Errors being thrown are a performance hit though, but who knows, the double.TryParse may be using the same technique. If you run some comparission tests let us know how it works out. Quote
Administrators PlausiblyDamp Posted August 5, 2005 Administrators Posted August 5, 2005 You could pass input as a string rather than object as .TryParse expects a string anyway. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
utilitaire Posted August 6, 2005 Posted August 6, 2005 Well you can always try converting the number in a try/catch. Errors being thrown are a performance hit though, but who knows, the double.TryParse may be using the same technique. If you run some comparission tests let us know how it works out. That's what I always do. I think this is quite performant, since: 1) as you said, Double.TryParse probably do a try/catch 2) the IsNumber() will probably be used most of the time with a number. Maybe 10% will generate a catch(). Depends how you use it, but If you actually expect a number, how many time the user will send a string that is non compatible? The user will send a number most of the time. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.