Results 1 to 10 of 10

Thread: Some Conditions are not executing

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    16

    Some Conditions are not executing

    Code:
    Dim userguess As Integer
        Dim compnumber As Integer
        Dim usercount As Integer
        Dim TestNumber As String
    
    
    
    
    
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Randomize()
    
            compnumber = Int((100 * Rnd()) + 1) '// Generate random value between 1 and 100.
    
            btnTry.Visible = False
    
    
        End Sub
    
        Private Sub btnGuess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuess.Click
    
    
            TestNumber = txtguess.Text
    
            If IsNumeric(TestNumber) = True Then ' check if string is a number value
                userguess = Val(TestNumber)
    
                If userguess > compnumber Then
                    txtmessage.Text = "Go Lower"
                    Me.BackColor = Color.Red
    
                ElseIf userguess < compnumber Then
                    txtmessage.Text = "Go Higher"
                    Me.BackColor = Color.Blue
    
    
                ElseIf userguess = compnumber Then
                    txtmessage.Text = "CORRECT"
                    Me.BackColor = Color.Yellow
    
    
    
                End If
    
            Else
                MsgBox(" You did not enter a numerical value ") ' if string value entered is a letter
            End If
    
            
    
            txtguess.Text = ""
            txtguess.Focus()
            usercount = usercount + 1
            txtcount.Text = usercount
    
            If userguess = compnumber And usercount < 5 Then
                MsgBox(" Either you know the secret, or you got lucky")
                btnTry.Visible = True
    
            ElseIf userguess = compnumber And usercount >= 5 & 6 & 7 Then
                MsgBox(" Aha, do you know the secret?")
                btnTry.Visible = True
    
            ElseIf userguess = compnumber And usercount >= 8 & 9 & 10 Then
                MsgBox(" Try to do better next time")
                btnTry.Visible = True
    
            End If
    
            If usercount >= 10 Then
                btnGuess.Enabled = False
                MsgBox(" You have taken too many guesses buddy")
                btnTry.Visible = True
    
    
            End If
    
                lstGuess.Items.Add(usercount & "                        " & userguess)
    
    
        End Sub
    
        Private Sub BtnHowToPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHowToPlay.Click
    
            MsgBox("Type a number between 0 and 100 into the box labeled Type Guess Here. The program will _generate a random number and will tell you if you've gone to high or to low, keep guessing until you are CORRECT! but you only have 10 guesses.")
        End Sub
    
        Private Sub btnTry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTry.Click
            btnGuess.Enabled = True
            txtguess.Text = ""
            Me.lstGuess.Text = " "
            txtmessage.Text = ""
            txtcount.Text = " "
            usercount = 0
            Randomize()
            compnumber = (Rnd() * 100)
            usercount = 0
            lstGuess.Items.Clear()
            Me.BackColor = Color.Empty
            lstGuess.Items.Add("Guess #" & "      " & "Number")
            txtguess.Focus()
    
        End Sub
    
        Private Sub Form_Load()
    
            Randomize()
            compnumber = (Rnd() * 100)
            usercount = 0
        End Sub
    End Class
    The conditions that display
    Code:
       If userguess = compnumber And usercount < 5 Then
                MsgBox(" Either you know the secret, or you got lucky")
                btnTry.Visible = True
    
            ElseIf userguess = compnumber And usercount >= 5 & 6 & 7 Then
                MsgBox(" Aha, do you know the secret?")
                btnTry.Visible = True
    
            ElseIf userguess = compnumber And usercount >= 8 & 9 & 10 Then
                MsgBox(" Try to do better next time")
                btnTry.Visible = True
    are not executing with my program and need help figuring out why.

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,835

    Re: Some Conditions are not executing

    For one thing 5 & 6 & 7 is really 567. Is that what you meant? Same with 8 & 9 & 10 - that is 8910.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    16

    Re: Some Conditions are not executing

    N this is not what I meant. I want 5 6 7 8 and 9 ad separate values to compare the counter with.

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    21

    Re: Some Conditions are not executing

    If you want "greater than 5,6, & 7 don't you want "greater than 7" ?

    Or do you want 5,6, OR 7, in which case I'd suggest a Case statement.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    16

    Re: Some Conditions are not executing

    I want the condition to apply when it is greater than or equal to 5 and less than on equal to 7 . Greater than or 7 and less than 10

  6. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    21

    Re: Some Conditions are not executing

    So write it that way...

    Try a case statement.

    Select case usercount
    Case 0 to 5
    .
    .
    Case 5 to 7
    .
    .
    Case 7 to 10
    .
    .

    http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Some Conditions are not executing

    try this:

    Code:
            ElseIf userguess = compnumber And usercount >= 8 Then
                MsgBox(" Try to do better next time")
                btnTry.Visible = True
            ElseIf userguess = compnumber And usercount >= 5 Then
                MsgBox(" Aha, do you know the secret?")
                btnTry.Visible = True
    If it's equal to or greater than 8, that fulfills the 8,9, or 10 condition.... otherwise it continues to the next if where it compares it to equal to or greater than 5 .....

    so if the user count is 7 .... it's not greater than 8, and so falls to the second one, where it is greater than 5...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Junior Member
    Join Date
    Jun 2012
    Posts
    21

    Re: Some Conditions are not executing

    Ya I started writing it the way technome did but was too lazy to write the explanation so I suggested Case

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Some Conditions are not executing

    since I'm pedantic and anal about certain coding techniques... I'd like to point out that the case statement as posted is incorrect... if the value is 7... which branch will it take? They shouldn't overlap... it should be 0 to 4, 5 to 7, 8 to 10.... the overlap "works" but only because it will take the first matching branch.,.. but form a readability & maintenance view, it's not so clear and someone doing a cursory glance at it may not distinguish the overlap and trace through the wrong path.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    Junior Member
    Join Date
    Jun 2012
    Posts
    21

    Re: Some Conditions are not executing

    Ya you're techgnome, for 7 there's 2 matches and it happens to pick the right one because of how it's laid out, but ya, shoulda been 8 to 10.

    Being pedantic is good sometimes .

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width