|
-
Oct 7th, 2015, 04:50 PM
#1
Thread Starter
New Member
Message Box error working for one type of error but not all
Good afternoon folks,
I stumbled on this forum during class and thought this would be the best place to get advice (my instructor was very vague and I didn't understand my questions).
The message box says that, "Number cannot be a letter and must be positive".
I have no issue with a negative number setting off the message box, but whenever I put a letter in the text box, no message pops up. I'll post my whole code for situation awareness, but the code that I'm having trouble will be at the bottom.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btbcalculate.Click
' Declare variables.
Dim intRunner1time As Integer
Dim intRunner2time As Integer
Dim intRunner3time As Integer
Dim strRunner1 As String = String.Empty
Dim strRunner2 As String = String.Empty
Dim strRunner3 As String = String.Empty
' Get input
Try
intRunner1time = CInt(txttime1.Text)
intRunner2time = CInt(txttime2.Text)
intRunner3time = CInt(txttime3.Text)
strRunner1 = txtRunner1.Text
strRunner2 = txtRunner2.Text
strRunner3 = txtRunner3.Text
'Display race results
If intRunner1time < intRunner2time And intRunner1time < intRunner3time And intRunner2time < intRunner3time Then
lblfirst.Text = strRunner1
lblsecond.Text = strRunner2
lblthird.Text = strRunner3
ElseIf intRunner1time < intRunner2time And intRunner1time < intRunner3time And intRunner3time < intRunner2time Then
lblfirst.Text = strRunner1
lblsecond.Text = strRunner3
lblthird.Text = strRunner2
ElseIf intRunner2time < intRunner1time And intRunner2time < intRunner3time And intRunner1time < intRunner3time Then
lblfirst.Text = strRunner2
lblsecond.Text = strRunner1
lblthird.Text = strRunner3
ElseIf intRunner2time < intRunner1time And intRunner2time < intRunner3time And intRunner3time < intRunner1time Then
lblfirst.Text = strRunner2
lblsecond.Text = strRunner3
lblthird.Text = strRunner1
ElseIf intRunner3time < intRunner1time And intRunner3time < intRunner2time And intRunner1time < intRunner2time Then
lblfirst.Text = strRunner3
lblsecond.Text = strRunner1
lblthird.Text = strRunner2
ElseIf intRunner3time < intRunner1time And intRunner3time < intRunner2time And intRunner2time < intRunner1time Then
lblfirst.Text = strRunner3
lblsecond.Text = strRunner2
lblthird.Text = strRunner1
End If
'display name errors
If txtrunner1.Text = String.Empty Then
MessageBox.Show("Please enter a name")
ElseIf txtrunner2.Text = String.Empty Then
MessageBox.Show("Please enter a name")
ElseIf txtrunner3.Text = String.Empty Then
MessageBox.Show("Please enter a name")
End If
'Display negative number and letter error
If Integer.TryParse(txttime1.Text, intRunner1time) And IsNumeric(intRunner1time) And intRunner1time < 0 Then
MessageBox.Show("Number cannot be a letter and must be positve")
ElseIf Integer.TryParse(txttime2.Text, intRunner2time) And IsNumeric(intRunner2time) And intRunner2time < 0 Then
MessageBox.Show("Number cannot be a letter and must be positve")
ElseIf Integer.TryParse(txttime3.Text, intRunner3time) And IsNumeric(intRunner3time) And intRunner3time < 0 Then
MessageBox.Show("Number cannot be a letter and must be positve")
End If
Catch ex As Exception
End Try
End Sub
-
Oct 7th, 2015, 04:56 PM
#2
Re: Message Box error working for one type of error but not all
Code:
Try
'...
'your code
Catch ex As Exception
Msgbox(ex.message)
End Try
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 7th, 2015, 05:01 PM
#3
Re: Message Box error working for one type of error but not all
Your code doesn't work because that error ceases execution of the Try part and jumps to the Catch part... -1,-2,etc are valid integers
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 8th, 2015, 09:04 AM
#4
Re: Message Box error working for one type of error but not all
It feels like maybe you saw several examples, then glued them together. You do a lot of the same work different ways within the same method.
The problem relevant to the question, as .paul. indicated, is that CInt() throws an exception when it fails to convert its argument to an Integer. This little block of code is evil as-written:
Code:
Catch ex As Exception
End Try
What that says is, "If any error happens at all, ignore it and pretend everything is fine." That's not usually what you want. You usually want to at least let yourelf know that an error happened. At the very least, you should modify it to this:
Code:
Catch ex As Exception
Console.WriteLine(ex)
End Try
That way you get some indication that something's gone wrong. But overall, many developers consider these catch-all blocks to be bad practice. Things work better when you can handle specific exceptions and do specific things in response. (Third-party APIs sometimes make this too difficult.)
Anyway, since you know your input can be invalid, it's not really safe to use CInt() on your strings. You could handle the exception, but usually people prefer to use other control flow. See further down where you use Integer.TryParse()? You should use it instead of CInt() at the top. In fact, that code sholud probably just be moved to the top. It looks like this right now:
Code:
Try
' Assume the input is good and do the math
' If the input was bad, print an error
Catch
' If an error happens, pretend it didnt.
End Catch
This is sort of like describing the process of backing out of a parking space as, "Put the car in reverse, accelerate, then look behind you to make sure nobody is standing behind you." That's a recipe for an accident. You want, "Look behind you, check for pedestrians, then put the car in reverse and accelerate.":
Code:
' TryParse() an integer, if it's invalid:
' Display an error message and stop.
' TryParse() the next integer...
'
' Do the math on all the integers, if we get here they are all valid.
You don't really need the Try..Catch if you reorganize that way.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Dec 11th, 2015, 05:41 PM
#5
Thread Starter
New Member
Re: Message Box error working for one type of error but not all
Thanks a lot everyone, sorry for very late reply, but your advice helped out greatly!
-
Dec 14th, 2015, 09:49 AM
#6
Re: Message Box error working for one type of error but not all
An example of getting an Integer from a TextBox without needing to use Try/Catch.
All in one go, showing only a single error message:
vb.net Code:
Dim Runner1TimeInteger As Integer
Dim Runner1timeString As String = txttime1.Text.Trim '<- Time1TextBox would be easier name to read
If Runner1timeString.Length > 0 AndAlso Integer.TryParse(Runner1timeString, Runner1TimeInteger) AndAlso Runner1TimeInteger > 0 Then
'You have valid input that's an integer that's positive
Else
MessageBox.Show("Please input a positive integer value", "Bad Input", MessageBoxButtons.OK, MessageBoxIcon.Error)
txttime1.Focus()
End If
If you need error messages broken out you can do this:
vb.net Code:
Dim Runner1TimeInteger As Integer
Dim Runner1timeString As String = txttime1.Text.Trim '<- Time1TextBox would be easier name to read
If Runner1timeString.Length > 0 Then
If Integer.TryParse(Runner1timeString, Runner1TimeInteger) Then
If Runner1TimeInteger > 0 Then
'You have valid input that's an integer that's positive
Else
MessageBox.Show("Please provide a positive integer", "Bad Input", MessageBoxButtons.OK, MessageBoxIcon.Error)
txttime1.Focus()
End If
Else
MessageBox.Show("Please integer input", "Bad Input", MessageBoxButtons.OK, MessageBoxIcon.Error)
txttime1.Focus()
End If
Else
MessageBox.Show("Please provide input", "Input Missing", MessageBoxButtons.OK, MessageBoxIcon.Error)
txttime1.Focus()
End If
If you have a lot of these textboxes I would recommend making a function to get the input, have it accept the TextBox as a parameter and handle the showing of the error message(s) and it would simply return the integer value (or 0 when bad input given).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|