Struggling to change the value of a class level variable

I’m very new to visual basic and am working on a simple tic tac toe program. I used a class level variable to control the whether clicking a button (one of the 9 spots) shows an “X” or an “O”. But I don’t seem to be able to influence the variable after declaring it. If the initial value is set to 0, then the text property of all the buttons clicked become “O”. If it’s set to 1, they all become “X”. Shouldn’t the “intTurn +=1” line be altering the variable?

Code:

Public Class frmMain Private intTurn As Integer Private Sub btnSpot1_Click(sender As Object, e As EventArgs) Handles btnSpot1.Click Dim intTurn As Integer = 1 If intTurn < 10 Then If intTurn = 0 Or 2 Or 4 Or 6 Or 8 Then btnSpot1.Text = "O" intTurn += 1 ElseIf intTurn = 1 Or 3 Or 5 Or 7 Or 9 Then btnSpot1.Text = "X" intTurn += 1 End If End If End Sub Private Sub btnSpot2_Click(sender As Object, e As EventArgs) Handles btnSpot2.Click If intTurn < 10 Then If intTurn = 0 Or 2 Or 4 Or 6 Or 8 Then btnSpot2.Text = "O" intTurn += 1 ElseIf intTurn = 1 Or 3 Or 5 Or 7 Or 9 Then btnSpot2.Text = "X" intTurn += 1 End If End If End Sub Private Sub btnSpot3_Click(sender As Object, e As EventArgs) Handles btnSpot3.Click If intTurn < 10 Then If intTurn = 0 Or 2 Or 4 Or 6 Or 8 Then btnSpot3.Text = "O" intTurn += 1 ElseIf intTurn = 1 Or 3 Or 5 Or 7 Or 9 Then btnSpot3.Text = "X" intTurn += 1 End If End If End Sub End Class 

This isn’t a complete version, but it shows what I’m trying to do with it. I’ve also tried a version like this:

Private Sub btnSpot1_Click(sender As Object, e As EventArgs) Handles btnSpot1.Click If intTurn = 0 Or 2 Or 4 Or 6 Or 8 Then btnSpot1.Text = "O" intTurn = intTurn + 1 Else btnSpot1.Text = "X" And intTurn = intTurn + 1 End If End Sub 

But it had the same problems. Every button clicked results in an “O”

Thanks

submitted by /u/neg4tivezer0
[link] [comments]

Leave a Reply