Click here to Skip to main content
Click here to Skip to main content

Tagged as

Validating Simple Primitive Data Types :TIP (For beginners)

, 13 Nov 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Validating Simple Primitive Data Types:TIP


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;
          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...


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


About the Author

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 :) PinmemberRaviSant18-Mar-11 11:15 
GeneralReason for my vote of 2 Sorry its repeated many times :) Pinmemberzenwalker198527-Feb-12 22:11 
GeneralI would say its a duplicate. :) Pinmemberzenwalker198527-Feb-12 22:09 
GeneralReason for my vote of 5 good one. Pinmembernikhi _singh27-Feb-12 19:22 
GeneralFor the beginners - you should include _why_ try / catch is ... PinmemberDoc Lobster26-Sep-11 0:50 
GeneralReason for my vote of 5 Good Tip. PinmemberRaviSant18-Mar-11 11:14 
GeneralRe: Thank you RaviSant PinmemberE$w@r18-Mar-11 11:15 
GeneralReason for my vote of 5 Good one Eswar Pinmvpthatraja6-Mar-11 6:59 
GeneralRe: Thank you Raja. PinmemberE$w@r6-Mar-11 7:12 
GeneralReason for my vote of 4 i like this PinmemberPranay Rana23-Dec-10 1:56 
Generalvery basic but good for beginners. PinmemberGPUToaster10-Nov-10 20:10 
GeneralMr JFergulbops, Thanks for suggestion. Modified. PinmemberEswa9-Nov-10 7:50 
General'For beginners' its in the title. try reading the Title. PinmemberJFergulbops8-Nov-10 23:44 
GeneralReason for my vote of 3 it is trivial PinmemberOnofrio Panzarino2-Nov-10 4:37 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 13 Nov 2010
Article Copyright 2010 by TweakBird
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid