Results 1 to 7 of 7

Thread: [RESOLVED] Else If[2005]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    27

    Resolved [RESOLVED] Else If[2005]

    For my dice game the the by continuing with else if it comes up with an error meaning that I can only roll two dice instead of six! HELP!!!


    Code:
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Turn = 1 Then
                DiceOne(DiceValue)
                lblTurn.Text = "Player One"
                Turn = 2
            Else
                DiceTwo(DiceValue)
                lblTurn.Text = "Player Two"
                Turn = 1
            Else If Turn = 3 Then
                DiceThree(DiceValue)
                lblTurn.Text = "Player One"
    
    
            End If
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Else If[2005]

    You can't have an "Else If" after an "Else". In that code it will enter the first block if Turn is equal to 1 and the second block otherwise, so the third block can never be reached. If you have an "Else" block it MUST be the last block of the series, so every other possibility has been checked first.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    27

    Re: Else If[2005]

    I understand what you mean, but do you have another solution for the six dice game?
    Thanks for the previous help!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Else If[2005]

    When you're testing for multiple values of the same variable it's generally preferable to use a Select Case statement:
    vb Code:
    1. Select Case Me.turn
    2.     Case 1
    3.  
    4.     Case 2
    5.  
    6.     Case 3
    7.  
    8.     Case 4
    9.  
    10.     Case 5
    11.  
    12.     Case 6
    13.  
    14. End Select
    15.  
    16. If Me.turn = 6 Then
    17.     Me.turn = 1
    18. Else
    19.     Me.turn += 1
    20. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    27

    Exclamation Re: Else If[2005]

    So for each Case statement I insert an If Statement?
    such as
    Code:
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Select Case Me.Turn
                Case 1
    If Turn = 1 Then
                DiceOne(DiceValue)
                lblTurn.Text = "Player One"
                Turn = 2
    
                Case 2
                Case 3
                Case 4
                Case 5
                Case 6
            End Select
            If Me.Turn = 6 Then
                Me.Turn = 1
            Else
                Me.Turn += 1
            End If
    And etc?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Else If[2005]

    No. The Select Case takes the place of the If. In English that's saying:
    Code:
    Select the case that matches the value of Me.turn
        In the case where Me.turn is 1 do the following
    
        In the case where Me.turn is 2 do the following
    etc., etc. Also, pay attention to the block at the bottom. That is incrementing the value of turn variable and setting it back to 1 if it reaches 6. You don't need to do that in each Case block. It should look like this:
    vb Code:
    1. Select Case Me.turn
    2.     Case 1
    3.         DiceOne(DiceValue)
    4.         lblTurn.Text = "Player One"
    5.     Case 2
    etc. That's all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    27

    Resolved Re: Else If[2005]

    Thanks! It works beautifully, now I got to make an application that records the score...Whoopy....

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