Click here to Skip to main content
11,641,224 members (65,141 online)
Click here to Skip to main content

Tagged as

Validating Simple Primitive Data Types :TIP (For beginners)

, 13 Nov 2010 CPOL 12.1K 8
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
GeneralRe: welcome :) Pin
RaviSant18-Mar-11 10:15
memberRaviSant18-Mar-11 10:15 
GeneralReason for my vote of 2 Sorry its repeated many times :) Pin
zenwalker198527-Feb-12 21:11
memberzenwalker198527-Feb-12 21:11 
GeneralI would say its a duplicate. :) Pin
zenwalker198527-Feb-12 21:09
memberzenwalker198527-Feb-12 21:09 
GeneralReason for my vote of 5 good one. Pin
nikhi _singh27-Feb-12 18:22
membernikhi _singh27-Feb-12 18:22 
GeneralFor the beginners - you should include _why_ try / catch is ... Pin
Doc Lobster25-Sep-11 23:50
memberDoc Lobster25-Sep-11 23:50 
GeneralReason for my vote of 5 Good Tip. Pin
RaviSant18-Mar-11 10:14
memberRaviSant18-Mar-11 10:14 
GeneralRe: Thank you RaviSant Pin
E$w@r18-Mar-11 10:15
memberE$w@r18-Mar-11 10:15 
GeneralReason for my vote of 5 Good one Eswar Pin
thatraja6-Mar-11 5:59
mvpthatraja6-Mar-11 5:59 
GeneralRe: Thank you Raja. Pin
E$w@r6-Mar-11 6:12
memberE$w@r6-Mar-11 6:12 
GeneralReason for my vote of 4 i like this Pin
Pranay Rana23-Dec-10 0:56
memberPranay Rana23-Dec-10 0:56 
Generalvery basic but good for beginners. Pin
GPUToaster10-Nov-10 19:10
memberGPUToaster10-Nov-10 19:10 
GeneralMr JFergulbops, Thanks for suggestion. Modified. Pin
Eswa9-Nov-10 6:50
memberEswa9-Nov-10 6:50 
General'For beginners' its in the title. try reading the Title. Pin
JFergulbops8-Nov-10 22:44
memberJFergulbops8-Nov-10 22:44 
GeneralReason for my vote of 3 it is trivial Pin
Onofrio Panzarino2-Nov-10 3:37
memberOnofrio Panzarino2-Nov-10 3: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
Web01 | 2.8.150731.1 | Last Updated 13 Nov 2010
Article Copyright 2010 by TweakBird
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid