Visual Studio 2012 Question/Help
Hi, so im making a Connect Four game, but im having trouble with the declaring the winner part. I have the pieces as back colors in picture boxes, but i cant figure out how to write the if statement saying if four picture boxes have the same back color.
for example i currently have:
Function Winner()
For rows = 0 To 5
For columns = 0 To 2
If picboxarray(rows, columns).BackColor = picboxarray(rows, columns + 1).BackColor And picboxarray(rows, columns + 1).BackColor = picboxarray(rows, columns + 2).BackColor And picboxarray(rows, columns + 2).BackColor And picboxarray(rows, columns + 3).BackColor And picboxarray(rows, columns).BackColor <> Nothing Then
Return True
End If
Next
Next
End Function
i get the error: Operator 'And' is not defined for types 'Boolean' and 'System.Drawing.Color'.
Any ideas on an alternative way? Thanks
Re: Visual Studio 2012 Question/Help
is this what you intended?:
Code:
If picboxarray(rows, columns).BackColor = picboxarray(rows, columns + 1).BackColor And picboxarray(rows, columns + 1).BackColor = picboxarray(rows, columns + 2).BackColor And picboxarray(rows, columns + 2).BackColor <> Nothing And picboxarray(rows, columns + 3).BackColor <> Nothing And picboxarray(rows, columns).BackColor <> Nothing Then
Return True
End If
Re: Visual Studio 2012 Question/Help
This line is an interesting one:
And picboxarray(rows, columns).BackColor <> Nothing Then
If .Backcolor is actually nothing, then ALL of the previous conditions will throw exceptions, so this particular part of the If statement had better be true. The better way to write that would be to put that part first, and use AndAlso rather than And. If you use And, then all parts of the expression will be evaluated. By using AndAlso, if the first part is False, then the rest aren't evaluated because the total result is already known.
As for the actual problem, you started out ok on that If statement, but then there is this part in the middle:
And picboxarray(rows, columns + 2).BackColor And
Note that it starts and ends with And, with no = sign in the middle. That is not what you meant to write, as you did it correctly for the first two by comparing the BackColor to some other color.
Re: Visual Studio 2012 Question/Help
Actually a picturebox BackColor can never be nothing. If you set it to Nothing it will revert to default (usually Color[Control]) so that part of the condition is useless as it stands.
Re: Visual Studio 2012 Question/Help
It will revert to the default value for the Color structure, which should be Black, but now that I think about it, you can't compare anything to Nothing using <> anyways. You'd have to use Is Not. Of course, compilation never got that far because of the missing part of the statement from earlier.
Re: Visual Studio 2012 Question/Help
using a picturebox straight from the toolbox with no modifications:
Code:
MsgBox(PictureBox1.BackColor <> Nothing) '=true
MsgBox(PictureBox1.BackColor <> SystemColors.Control) '=false
Re: Visual Studio 2012 Question/Help
thanks everyone i found out my problem. feel free to check out my new thread once it gets accepted to be put up