Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I know that this has been addressed before, but I just can't seem to flush out the correct way to handle the error checking for this. Any help or guidance would be greatly appreciated! The code is supposed to work so that a error message box is shown if a wrong number is entered and is specific to each input box. As it is now, it doesn't throw any exceptions, but it doesn't work at all, either. All it shows is the "The PIN is valid" when Verify is clicked.

VB
Private Sub btnVerify_Click(sender As Object, e As EventArgs) Handles btnVerify.Click
       'Declare Variables and arrays
       Dim intMinimum() As Integer = {7, 5, 0, 0, 6, 3, 4} 'Holds Minimum range.
       Dim intMaximum() As Integer = {9, 7, 4, 9, 9, 6, 8} 'Holds Maximum range.
       Dim strTextboxes() As String = {txtBox1.Text, txtBox2.Text, txtBox3.Text, txtBox4.Text, txtBox5.Text, txtBox6.Text, txtBox7.Text}
       Dim intNumber As Integer
       Dim blnRange As Boolean = True
       Dim intCount As Integer



       For intCount = 0 To 6
           If intCount = 0 Then
               If Integer.TryParse(strTextboxes(intCount), intNumber) Then
                   If Not (intNumber >= intMinimum(intCount) And intNumber <= intMaximum(intCount)) Then
                       MessageBox.Show("Enter a number between 7 and 9", "Digit 1")
                       Return
                   End If
               End If
           ElseIf intCount = 1 Then
               If Integer.TryParse(strTextboxes(intCount), intNumber) Then
                   If Not (intNumber >= intMinimum(intCount) And intNumber <= intMaximum(intCount)) Then
                       MessageBox.Show("Enter a number between 5 and 7", "Digit 2")
                       Return
                   End If
               End If
           ElseIf intCount = 2 Then
               If Integer.TryParse(strTextboxes(intCount), intNumber) Then
                   If Not (intNumber >= intMinimum(intCount) And intNumber <= intMaximum(intCount)) Then
                       MessageBox.Show("Enter a number between 0 and 4", "Digit 3")
                       Return
                   End If
               End If
           ElseIf intCount = 3 Then
               If Integer.TryParse(strTextboxes(intCount), intNumber) Then
                   If Not (intNumber >= intMinimum(intCount) And intNumber <= intMaximum(intCount)) Then
                       MessageBox.Show("Enter a number between 0 and 9", "Digit 4")
                       Return
                   End If
               End If
           ElseIf intCount = 4 Then
               If Integer.TryParse(strTextboxes(intCount), intNumber) Then
                   If Not (intNumber >= intMinimum(intCount) And intNumber <= intMaximum(intCount)) Then
                       MessageBox.Show("Enter a number between 6 and 9", "Digit 5")
                       Return
                   End If
               End If
           ElseIf intCount = 5 Then
               If Integer.TryParse(strTextboxes(intCount), intNumber) Then
                   If Not (intNumber >= intMinimum(intCount) And intNumber <= intMaximum(intCount)) Then
                       MessageBox.Show("Enter a number between 3 and 6", "Digit 6")
                       Return
                   End If
               End If
           ElseIf intCount = 6 Then
               If Integer.TryParse(strTextboxes(intCount), intNumber) Then
                   If Not (intNumber >= intMinimum(intCount) And intNumber <= intMaximum(intCount)) Then
                       MessageBox.Show("Enter a number between 4 and 8", "Digit 7")
                       Return
                   End If
               End If
               Else
                   MessageBox.Show("Not a valid PIN")
                   Return
               End If
       Next

       MessageBox.Show("The PIN is valid")

   End Sub
Posted
Updated 16-Nov-14 14:46pm
v2
Comments
Member 11139267 16-Nov-14 22:08pm    
Hi dehumanate,

Thank you so much!

"If this line fails to parse the number, it will return false, skipping to the end of the loop and continuing as if everything is OK."

That makes absolute sense. I was assuming that a number would be entered, never assume!!
Also, the huge loop was messy, I just seriously was at a loss on how to clean it up and still have it work.

Oh, the Dim blnRange As Boolean = True should have been removed, I had tried a couple of variations for the code. This was part of a code that didn't work, either.

Again, many thanks, your code is perfect and I learned something, YAY!

1 solution

Ok, let's see if, without using an IDE or a VB compiler, we can find some bugs!

Firstly, let's simplify that huge loop - notice how ever block after each If statement checking intCount is the same? We can refactor this to use the loop effectively:

VB
Private Sub btnVerify_Click(sender As Object, e As EventArgs) Handles btnVerify.Click
    'Declare Variables and arrays
    Dim intMinimum() As Integer = {7, 5, 0, 0, 6, 3, 4} 'Holds Minimum range.
    Dim intMaximum() As Integer = {9, 7, 4, 9, 9, 6, 8} 'Holds Maximum range.
    Dim strTextboxes() As String = {txtBox1.Text, txtBox2.Text, txtBox3.Text, txtBox4.Text, txtBox5.Text, txtBox6.Text, txtBox7.Text}

    ' what is this variable blnRange for?
    'Dim blnRange As Boolean = True

    Dim intNumber As Integer
    Dim intCount As Integer
       
    For intCount = 0 To 6
        If Integer.TryParse(strTextboxes(intCount), intNumber) Then
            If Not (intNumber >= intMinimum(intCount) And intNumber <= intMaximum(intCount)) Then
                ' format the message with the loop variables
                ' make this code cleaner by putting the parameters
                ' into variables (reduces bracket counting for the
                ' poor souls reading it)
                MessageBox.Show(String.Format("Enter a number between {0} and {1}",intMinimum(intCount), intMaximum(intCount)), String.Format("Digit {0}", intNumber+1))
                Return
            End If
        End If
    Next
   
    MessageBox.Show("The PIN is valid")
 
End Sub


Now that we have cleaned up the code, a potential logical error becomes very obvious. The first line in the loop tries to parse the text box.

VB
For intCount = 0 To 6
    ' vv this line here vv
    If Integer.TryParse(strTextboxes(intCount), intNumber) Then
    ' do stuff
    End If
Next


If this line fails to parse the number, it will return false, skipping to the end of the loop and continuing as if everything is OK. Which leads us to:

VB
MessageBox.Show("The PIN is valid")


Which is the behaviour your described. You need to make sure that if Integer.TryParse fails that it is handled correctly. Something like:

VB
For intCount = 0 To 6
    ' first check we have a digit
    If Not Integer.TryParse(strTextboxes(intCount), intNumber) Then
        MessageBox.Show("Please enter a number only.", String.Format("Digit {0}", intCount+1))
        Return
    End If

    ' now check it fits our requirements
    If Not (intNumber >= intMinimum(intCount) And intNumber <= intMaximum(intCount)) Then
        MessageBox.Show(String.Format("Enter a number between {0} and {1}",intMinimum(intCount), intMaximum(intCount)), String.Format("Digit {0}", intNumber+1))
        Return
    End If
Next


Something like that, anyhow. Please keep in mind I'm a C# guy, I haven't done VB code since VB6 like 15 odd years ago, and I didn't use a compiler to check any of this code.

In my experience, logical/flow bugs like this come down to simplify the code as much as possible, and then if you are using Visual Studio your IDE learn how to use the debugger properly (step-into and quick watch in VS would've solved this for you in ~30 seconds). If you aren't using VS, use VS. Seriously. (for .NET development, anyhow)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900