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

Tagged as

Go to top

Validating Simple Primitive Data Types :TIP (For beginners)

, 13 Nov 2010
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.

Comments and Discussions

 
GeneralRe: welcome :) PinmemberRaviSant18-Mar-11 10:15 
GeneralReason for my vote of 2 Sorry its repeated many times :) Pinmemberzenwalker198527-Feb-12 21:11 
GeneralI would say its a duplicate. :) Pinmemberzenwalker198527-Feb-12 21:09 
GeneralReason for my vote of 5 good one. Pinmembernikhi _singh27-Feb-12 18:22 
GeneralFor the beginners - you should include _why_ try / catch is ... PinmemberDoc Lobster25-Sep-11 23:50 
GeneralReason for my vote of 5 Good Tip. PinmemberRaviSant18-Mar-11 10:14 
GeneralRe: Thank you RaviSant PinmemberE$w@r18-Mar-11 10:15 
GeneralReason for my vote of 5 Good one Eswar Pinmvpthatraja6-Mar-11 5:59 
GeneralRe: Thank you Raja. PinmemberE$w@r6-Mar-11 6:12 
GeneralReason for my vote of 4 i like this PinmemberPranay Rana23-Dec-10 0:56 
Generalvery basic but good for beginners. PinmemberGPUToaster10-Nov-10 19:10 
GeneralMr JFergulbops, Thanks for suggestion. Modified. PinmemberEswa9-Nov-10 6:50 
General'For beginners' its in the title. try reading the Title. PinmemberJFergulbops8-Nov-10 22:44 
GeneralReason for my vote of 3 it is trivial PinmemberOnofrio 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 | Mobile
Web03 | 2.8.140922.1 | Last Updated 13 Nov 2010
Article Copyright 2010 by TweakBird
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid