3 Replies - 4756 Views - Last Post: 11 October 2009 - 07:54 PM Rate Topic: -----

#1 ellevador  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 01-March 09

IsNumeric vs TryParse

Post icon  Posted 11 October 2009 - 07:09 PM

On all my assignments I am asked to make sure the data is valid- only numeric, no empty strings. And I keep wondering which is the best to use IsNumeric or TryParse. When I try both on coding they seem to do the same thing, and I don't seem to find my answer online. Any ideas of why to use one over the other..
Is This A Good Question/Topic? 0
  • +

Replies To: IsNumeric vs TryParse

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: IsNumeric vs TryParse

Posted 11 October 2009 - 07:26 PM

They are pretty much the same. Though I do prefer TryParse over IsNumeric. The reason is that if you use TryParse you also get the result of the conversion if it is successful. With IsNumeric you would have to do the conversion after you find out if the value is numeric.
Was This Post Helpful? 1
  • +
  • -

#3 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: IsNumeric vs TryParse

Posted 11 October 2009 - 07:48 PM

IsNumeric lives in the Microsoft.VisualBasic dll file, whereas the TryParse methods are part of the Base Class Library - so if you want to learn other .Net languages, it is better to stick to TryParse as you can transfer your skills.

IsNumeric accepts object - you could feed it a Button or a Form and it wouldn't complain at compile time. The TryParse methods are strongly typed - you have to send it a String or it will not compile. It's better to use TryParse as it will catch dumb mistakes earlier.

IsNumeric isn't very specific, if you want an Integer, the user could type in a double and it would pass. You have specific TryParse methods for Integer, Double, Single, Byte, DateTime, ...

Usually the BCL methods are faster than ones from the vb dll.
Was This Post Helpful? 1
  • +
  • -

#4 ellevador  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 01-March 09

Re: IsNumeric vs TryParse

Posted 11 October 2009 - 07:54 PM

Thanks guys, exactly the information I needed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1