Question Do until loop not executing

mcannata

Member
Joined
Nov 16, 2010
Messages
5
Programming Experience
Beginner
Hey guys first post. My question is I have a program that is used to add inputs to an array when the OK button is clicked. My problem is that the after the first click of the OK button the Do while loop does not execute again for a 2nd click. Any help would be great this assignment has me pulling out my hair. Here is the code.

VB.NET:
Public Class InputValue
    Public Shared InputArray(0 To 9) As Integer


 





    

    Private Sub OkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OkButton.Click
        Dim LoopCounterInteger As Integer
        Dim InputInteger As Integer
        Dim CounterInteger As Integer

        LoopCounterInteger = 0

        Do While LoopCounterInteger < 1

            Try
                InputInteger = Integer.Parse(UserInputTextBox.Text)
            Catch ex As Exception

            End Try

            LoopCounterInteger += 1

            InputValueLabel.Text = "Please enter value # " & CounterInteger + 1 & " of 10:"

            InputArray(CounterInteger) = InputInteger

            CounterInteger = CounterInteger + 1

        Loop

        If CounterInteger = 10 Then

            For Each value As String In InputArray

                CannataArrayValues.InputListBox.Items.Add(value)
                Me.Close()

            Next

        End If

    End Sub

    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Me.Close()
    End Sub
End Class
 
Last edited by a moderator:
Your loop doesn't make sense. You are starting a value as 0 and looping while it's less than 1, but you always increment it inside the loop. That is always going to do one iteration of the loop no matter what, so what's the point of the loop? I think you need to rethink your logic. Maybe you should provide a clear, step-by-step explanation of exactly what you're trying to achieve.
 
Every time i click the OK button the code is supposed to be executed and the value added to the array element that corresponds with the counter variable. When i click the OK button it works, but on the next click of the OK button nothing happens. Should i even be using a loop? What could be the problem with my ok button? I appreciate the response.
 
You use a loop when you want to do the same thing multiple times. Each time you click the Button, do you want to do something once or multiple times? It sounds like you want once per Click, so there should be no loop. To perform your action multiple times you click the Button multiple times.
 
Well i took away the loop and the code now looks like this, but the OK button only works once. I dont get why the OK button would only work once.
VB.NET:
 Private Sub OkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OkButton.Click


        Dim InputInteger As Integer
        Dim CounterInteger As Integer

        Try
            InputInteger = Integer.Parse(UserInputTextBox.Text)
        Catch ex As Exception
        End Try


        InputValueLabel.Text = "Please enter value # " & CounterInteger + 1 & " of 10:"


        InputArray(CounterInteger) = InputInteger


        CounterInteger = CounterInteger + 1

        If CounterInteger = 10 Then


            For Each value As String In InputArray

                CannataArrayValues.InputListBox.Items.Add(value)
                Me.Close()

            Next

        End If

    End Sub
End Class
 
Last edited by a moderator:
I've now added
VB.NET:
 formatting tags to your posts twice in this thread.  Please add them yourself when posting code snippets in future.  The advanced editor has a button for the purpose.

The code is working every time.  It's just that that code doesn't do what you want it to do.  Have you checked what the actual values of your variables are?  Debugging consists of more than just looking at your code.  If nothing seems immediately obvious then you need to run the code and check what it's doing while it's in action.  The first step in that is to check the values of your variables at various points to make sure they are what you expect them to be.
 
I dont know what that means Formatting tags. When you added these it worked for you every single time? More than just once? If you could be so kind as to show me what you did. It would be very helpful ive put in more than i can spend on this minor issue. I have been forced to move on to other projects. So please could you share your solution.
 
Resolved my counter was resetting to "0" every time i clicked the OK button. Solved by making a global variable from the first form to hold counter value. Thanks for the help you pointed me in the right direction with the last post.
 
I'm glad to see that you took the direction provided and solved the problem for yourself. That was my intention as I think it's a far better way for people to improve their skills than being spoon fed a working solution. It's harder in the short term but far more rewarding in the long term.
 
Back
Top