Try Catch Statement help

  • Thread starter Thread starter Grant
  • Start date Start date
G

Grant

I want to be able to accept a value in "goToTextBox.Text" if it is a decimal
value such as 3.4. My Try statement currently catches non-numeric or blank
data, which I want to retain. How can I accomplish this?

T.I.A.
Grant



Try
Dim goToInteger As Integer = Integer.Parse(goToTextBox.Text) - 1

If goToInteger < 0 Or goToInteger > countInteger Then
Statements........

Else
Statements........

End If
Catch theException As Exception
MessageBox.Show("Non numeric data or blank field.", "Selection
Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
With goToTextBox
.Focus()
.SelectAll()
End With
End Try
 
Strictly speaking you shouldn't be trying to throw an exception here as there
is quite an overhead involved in raising exceptions...

I would use a boolean flag, something like:

Dim isGood As Boolean

isGood = False

If CInt(goToTextBox.Text) Then
isGood = True
Else
isGood = False
End If

If CDbl(goToTextBox.Text) Then
isGood = True
Else
isGood = False
End If

If Not isGood Then
MsgBox etc...
End If

HTH
 
Hi,
A suggestion :

If IsNumeric(goToTextBox.Text) Then
Statements........
Else
MessageBox.Show("Non numeric data or blank field.", "Selection Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With goToTextBox
.Focus()
.SelectAll()
End With
End If

Hope this help; Regards.
 
MN at2 dot3 > said:
A suggestion :

If IsNumeric(goToTextBox.Text) Then
Statements........

Notice that 'IsNumeric' will return 'True' for all valid numbers, but
doesn't take account if the number represented by the string can be stored
(parsed) in a certain numeric data type.
 
Back
Top