For loop

For loop

Page 1 of 1

2 Replies - 1549 Views - Last Post: 17 March 2008 - 04:37 PM Rate Topic: -----

#1 vber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-March 08

For loop

Posted 17 March 2008 - 02:20 PM

I have this loop that checks a textbox against PINs in a database to see if they can continue in the program. However if the PIN in the textbox is wrong a have a message box that appears to say the pin was wrong but with my loop a message appears for each of the PINs when you enter a wrong PIN. Any ideas?

   Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
        Dim menu As New frmATMMenu



        For i As Integer = 0 To objDS.Tables(0).Rows.Count - 1 ' loops through the PINs in the table

            If txtPIN.Text = DirectCast(objDS.Tables(0).Rows(i)("PIN"), System.Int32) Then

                Me.Hide()

                menu.Show()

            ElseIf txtPIN.Text <> DirectCast(objDS.Tables(0).Rows(i)("PIN"), System.Int32) Then

                MessageBox.Show("Wrong PIN entered. Please try again.", "Wrong PIN", MessageBoxButtons.OK, MessageBoxIcon.Hand)


            End If

        Next



    End Sub


*edit: Modified code tags for proper display. Thanks for using tags. :)

This post has been edited by Martyr2: 17 March 2008 - 02:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: For loop

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4404
  • View blog
  • Posts: 12,260
  • Joined: 18-April 07

Re: For loop

Posted 17 March 2008 - 02:38 PM

Each time through the loop you are obviously not going to match the pin in the database. So you should only do the hide show when you have found the pin in the list and nothing else if it doesn't match. If they make it out of the loop without having hit the good pin, obviously they have a bad pin and then you show the message.

You can accomplish this behavior using a flag variable. We set it to false first and during our loop if we found the pin, we make the flag true. If we don't find the pin, it will never be set to true so then after the for loop is finished, we can test this flag to see if we need to show the message of bad pin.

Here is an example....

Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
        Dim menu As New frmATMMenu

        ' This "flag" variable tells us if we have found a match.
        Dim validpin as Boolean = false


        For i As Integer = 0 To objDS.Tables(0).Rows.Count - 1 ' loops through the PINs in the table

            If txtPIN.Text = DirectCast(objDS.Tables(0).Rows(i)("PIN"), System.Int32) Then

                ' Yes, we found a match, so set it to true.
                validpin = true

                Me.Hide()

                menu.Show()

            End If

        Next

        ' Notice here we check if validpin is still false (not been found so never was set to true)
        If validpin = false then
                MessageBox.Show("Wrong PIN entered. Please try again.", "Wrong PIN", MessageBoxButtons.OK, MessageBoxIcon.Hand)
        End if

End Sub



As you can see we test the flag after the loop and to see if the pin had been found. Otherwise we would be showing a message for every bad match, even if the good pin is in the list.

Hopefully this makes sense and solves your problem. Enjoy!

"At DIC we be flag throwing code ninjas!" :snap:

This post has been edited by Martyr2: 17 March 2008 - 02:39 PM

Was This Post Helpful? 0
  • +
  • -

#3 vber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-March 08

Re: For loop

Posted 17 March 2008 - 04:37 PM

thanks that was excellent help. it works now

This post has been edited by PsychoCoder: 17 March 2008 - 05:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1