Click here to Skip to main content
15,796,025 members
Articles / General Programming
Tip/Trick

Validating Simple Primitive Data Types :TIP (For beginners)

Rate me:
Please Sign up or sign in to vote.
4.46/5 (13 votes)
13 Nov 2010CPOL 25K   8   14
Validating Simple Primitive Data Types:TIP

Introduction



In general, most of the developers are not aware of the inbuilt methods available for validating the primitive data types like System.Int32 and end up doing a custom implementation. The function below is a sample of that custom implementation to validate whether the given string is numeric or not.

public bool CheckIfNumeric(string value)
  {
      bool isNumeric = true;
      try
      {
          int i = Convert.ToInt32(value);
      }
      catch(FormatException exception)
      {
          isNumeric = false;
      }
      return isNumeric;
  }


Since it involves try catch, it is not the best way. A better approach would be to use int.TryParse as shown below:

int output = 0;
  bool isNumeric = int.TryParse(value, out output);


int.TryParse, in my opinion, is definitely the faster and cleaner way.

Keep a watch for more...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
I have good knowledge on Application Development, Developing components, Participating in design workshops and reviews. Particularly interested in client/server and windows applications. i have working experience with Oracle 10g, 11g, PostgreSQL, and MS-SQL Server Databases.

Comments and Discussions

 
GeneralRe: welcome :) Pin
Ravi Sant18-Mar-11 11:15
Ravi Sant18-Mar-11 11:15 
GeneralReason for my vote of 2 Sorry its repeated many times :) Pin
zenwalker198527-Feb-12 22:11
zenwalker198527-Feb-12 22:11 
GeneralI would say its a duplicate. :) Pin
zenwalker198527-Feb-12 22:09
zenwalker198527-Feb-12 22:09 
GeneralReason for my vote of 5 good one. Pin
Nikhil_S27-Feb-12 19:22
professionalNikhil_S27-Feb-12 19:22 
GeneralFor the beginners - you should include _why_ try / catch is ... Pin
Doc Lobster26-Sep-11 0:50
Doc Lobster26-Sep-11 0:50 
GeneralReason for my vote of 5 Good Tip. Pin
Ravi Sant18-Mar-11 11:14
Ravi Sant18-Mar-11 11:14 
GeneralRe: Thank you RaviSant Pin
TweakBird18-Mar-11 11:15
TweakBird18-Mar-11 11:15 
GeneralReason for my vote of 5 Good one Eswar Pin
thatraja6-Mar-11 6:59
professionalthatraja6-Mar-11 6:59 
GeneralRe: Thank you Raja. Pin
TweakBird6-Mar-11 7:12
TweakBird6-Mar-11 7:12 
GeneralReason for my vote of 4 i like this Pin
Pranay Rana23-Dec-10 1:56
professionalPranay Rana23-Dec-10 1:56 
Generalvery basic but good for beginners. Pin
GPUToaster™10-Nov-10 20:10
GPUToaster™10-Nov-10 20:10 
GeneralMr JFergulbops, Thanks for suggestion. Modified. Pin
TweakBird9-Nov-10 7:50
TweakBird9-Nov-10 7:50 
Mr JFergulbops,
Thanks for suggestion. Modified.
General'For beginners' its in the title. try reading the Title. Pin
Sherylee8-Nov-10 23:44
Sherylee8-Nov-10 23:44 
GeneralReason for my vote of 3 it is trivial Pin
Onofrio Panzarino2-Nov-10 4:37
Onofrio Panzarino2-Nov-10 4:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.