VB 2008 - Convert contents of TextBox to an Integer

ActiveAns is a textbox.  It may contain Nothing or an integer.  If it contains a number I need to convert that to (n) as below.  I can't seem to do this without runtime errors that say it cannot convert it.  

How do I test ActiveAns and then convert the contents of ActiveAns to n ?

If IsNumeric(ActiveAns.Text) And ActiveAns.Text > 0 Then
    Dim n As Integer = Int(ActiveAns.Text)
    blah blah blah
End If

Open in new window

Who is Participating?
Wayne Taylor (webtubbs)Connect With a Mentor Commented:
Use the Integer.TryParse method....

        Dim n As Integer
        If Integer.TryParse(ActiveAns.Text, n) Then
            MsgBox(n.ToString & " is an integer")
            MsgBox("is not an integer")
        End If

If IsNumeric(ActiveAns.Text) And ActiveAns.Text > 0 Then
    Dim n As Integer = Int(ActiveAns.Text)
    blah blah blah
catch ex as exception
Msgbox("Entry must be a number")
end try
End If

ljcorAuthor Commented:
Thanks to each of you for your advice.  I didn't use the second solution because I thought I had used it earlier without a result.
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.