How to check if a value is a integer.

Hi there

Wonder if you can help me?

Im pulling out some information from a SQL table, one of the fields in SQL may contain an integer or char's or alpha numeric.

so

I could have

12   or   testing    or    testing123   or testing 123

How do I check if the value is an integer? So the code will do something if its 12 but do something else if its testing 123 or testing123 or testing.

hope that makes sense?
Mousemat24
mousemat24Asked:
Who is Participating?
 
Priest04Commented:
It is not a matter what is more elegant, but what is more efficient.. Do some benchmarking, have a for... next loop and try it both ways, and see how much faster is TryParse. As for the example

int x;
if (double.TryParse(SomeStringValue, out x))
{
    // if this part of code runs then parsing to integer was successful
    // and integer values is stored in x variable.
}
else
{
    // if this part of code runs then parsing to integer failed
}

SomeStringValue is a value from your database, and must be a string type. If it isnt cast it to string, either using Convert.ToString, or ToString() method

Goran
0
 
Priest04Commented:
You can use Int32.TryParse method. If it fails, return value is false, and no exception is raised

Goran
0
 
Priest04Commented:
Or you can create a custom IsNumeric function, that will loop through all chars and use char method isDigit to  check if it is numeric.

Goran
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
YurichCommented:
There are several way. Unfortunatelly, we still don't have IsInteger or IsNumber as they do in VB.NET :|, so the code snippet will be:

private bool IsNumber( string str )
{
      try
      {
            int x = Convert.ToInt32( str );
            return true;
      }
      catch
      {
            return false;
      }
}

There're might be some other ways, but they all will be similar in concept.

Cheers,
Yurich
0
 
Priest04Commented:
Throwing an exception is the last option, since it is much slower that using int32.TryParse

Goran
0
 
YurichCommented:
goran, are you addressing to me? if yes, where do you see exception throwing?

that's an exception throwing:

throw new Exception( "my exception message" );

but TryParse does indeed looks more elegant and it's less coding.

cheers,
yurich
0
 
YurichCommented:
sorry, it's indeed. disregard first three sentences of my previous post.
0
 
mousemat24Author Commented:
Hi there, you've lost me now, should I use

private bool IsNumber( string str )
{
      try
      {
            int x = Convert.ToInt32( str );
            return true;
      }
      catch
      {
            return false;
      }
}

or not? You say I should use TryParse, can you please send me some code please?

Many thanks for helping me out
Mousemat24
0
 
Hitesh ManglaniCommented:
0
 
YurichCommented:
You can use try-catch combo, but more elegant way would be:

private bool IsNumber( string str )
{
      int result = Int32.MinValue;
      return ( Int32.TryParse( str, result ) == null ) ? false : true;
}

Cheers,
Yurich
0
 
mousemat24Author Commented:
Thanks guys for helping out!! :-)
0
 
YurichCommented:
pleasure :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.