Check if variable is an integer

How do you check if a variable is an integer? In vb.net 2.0 Thanks
wilcor14Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YiogiCommented:
I assume you have a string and want to check if its numeric.

If (Integer.TryParse(yourstringhere, numbertosavenumberinifitcanbeparsed)) Then
  ' Do whatever you want your string is an integer and saved in numbertosavenumberinifitcanbeparsed
Else
 ' your number is not an integer take appropriate actions
End If

Of course you can also use tryparse with other data types not just int.
0
wilcor14Author Commented:
That didn't seem to work.

Basically all I need is to check if a variable is an integer and if not, do nothing so eg.

sID = "301a"

If (Integer.TryParse(sID, ??)) Then
  ' Do whatever you want your string is an integer and saved in numbertosavenumberinifitcanbeparsed
Else
 ' your number is not an integer take appropriate actions
End If

Thanks
0
YiogiCommented:

Dim sID As String
Dim intToSavesID As Integer
 
If (Integer.TryParse(sID, intToSavesID)) Then
  ' Do whatever you want your string is an integer and saved 
  ' in intToSavesID variable.
Else
 ' your number is not an integer take appropriate actions
End If

Open in new window

0
YiogiCommented:
grr. forgot your line of assigning sID a value
Dim sID As String
Dim intToSavesID As Integer
 
sID = "301a"
 
If (Integer.TryParse(sID, intToSavesID)) Then
  ' Do whatever you want your string is an integer and saved 
  ' in intToSavesID variable.
Else
 ' your number is not an integer take appropriate actions
 
End If

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wilcor14Author Commented:
OK I get it now. Sorry. That works perfect. Thanks for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.