• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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
0
mousemat24
Asked:
mousemat24
  • 5
  • 4
  • 2
  • +1
2 Solutions
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
hiteshgoldeneyeCommented:
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
 
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
 
mousemat24Author Commented:
Thanks guys for helping out!! :-)
0
 
YurichCommented:
pleasure :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now