Try Catch Statement help

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
 
G

Guest

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
 
M

MN

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.
 
H

Herfried K. Wagner [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top