0

New to VB, trying to do some calculation codings in VB , but it doesn't seem to work?

I've stored the integer in to 'Access'.

      Dim max_number As Integer = 63

    If Access > max_number Then
        Access = max_number
    End If

    While Access > max_number Or Access >= 0

        If Access - 32 >= 0 Then
            Access = Access - 32
            Text1.Text = "1"
        ElseIf Access - 16 >= 0 Then
            Access = Access - 16
            Text2.Text = " 1"
        ElseIf Access - 8 >= 0 Then
            Access = Access - 8
            Text3.Text = "1"
        ElseIf Access - 4 >= 0 Then
            Access = Access - 4
            Text4.Text = "1"
        ElseIf Access - 2 >= 0 Then
            Access = Access - 2
            Text5.Text = "1"
        ElseIf Access - 1 >= 0 Then
            Access = Access - 1
            Text6.Text = " 1"
        End If
          Access = 0

    End While

Many Thanks.

7
  • What is the error you get? Commented Nov 25, 2013 at 8:59
  • Errrr, the software just stops there. and I can't click anything else except for the close button . Commented Nov 25, 2013 at 9:01
  • What is your expected outcome? What actually happens? Commented Nov 25, 2013 at 9:04
  • Expected outcome, example: if user input Number = 63, then the 6 text-boxes will have '1' ? so it will continuing looping until it minus until 0. Commented Nov 25, 2013 at 9:23
  • ...And how do you expect to exit the loop if at the end of the iteration you set the Access value to "0" again?. Remove that, the loop's logic is wrong. Commented Nov 25, 2013 at 9:26

2 Answers 2

1

Remove "Access = 0" in your loop and change the loop to and you don't need to test for max_number since it's already done before.

While Access > 0

If you try to run it on paper, you'll notice that if Access is equal to 2. It will go in your if (do Access = Access - 2) and Access will be equal to 0, you'll be in an infite loop.

But you don't need a while loop.

Dim max_number As Integer = 63

If Access > max_number Then
    Access = max_number
End If

If Access > 0 Then
    If (Access And 32) > 0 Then Text1.Text = "1"
    If (Access And 18) > 0 Then Text2.Text = "1"
    If (Access And 8) > 0 Then Text3.Text = "1"
    If (Access And 4) > 0 Then Text4.Text = "1"
    If (Access And 2) > 0 Then Text5.Text = "1"
    If (Access And 1) > 0 Then Text6.Text = "1"
End If
Sign up to request clarification or add additional context in comments.

Comments

0

You are getting stuck in an infinite loop because you keep setting Access to zero at the end of your loop.

Also, I don't know exactly what you are trying to do, but you may want to look into the Mod operator because it will probably reduce the number of iterations you will need through your loop.

Anyways, to break the infinite loop, try this:

Dim max_number As Integer = 63

If Access > max_number Then
    Access = max_number
End If

While Access > max_number Or Access >= 0

    If Access - 32 >= 0 Then
        Access = Access - 32
        Text1.Text = "1"
    ElseIf Access - 16 >= 0 Then
        Access = Access - 16
        Text2.Text = " 1"
    ElseIf Access - 8 >= 0 Then
        Access = Access - 8
        Text3.Text = "1"
    ElseIf Access - 4 >= 0 Then
        Access = Access - 4
        Text4.Text = "1"
    ElseIf Access - 2 >= 0 Then
        Access = Access - 2
        Text5.Text = "1"
    ElseIf Access - 1 >= 0 Then
        Access = Access - 1
        Text6.Text = " 1"
    Else
        Exit While
    End If

End While

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.