|
-
Apr 22nd, 2007, 09:56 PM
#1
Thread Starter
Junior Member
[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
-
Apr 22nd, 2007, 10:25 PM
#2
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.
-
Apr 23rd, 2007, 10:20 PM
#3
Thread Starter
Junior Member
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!
-
Apr 23rd, 2007, 10:51 PM
#4
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:
Select Case Me.turn
Case 1
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
-
Apr 24th, 2007, 09:55 PM
#5
Thread Starter
Junior Member
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?
-
Apr 24th, 2007, 10:00 PM
#6
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:
Select Case Me.turn
Case 1
DiceOne(DiceValue)
lblTurn.Text = "Player One"
Case 2
etc. That's all.
-
Apr 24th, 2007, 10:16 PM
#7
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|