i've 9 button(i've set "" to button text) there and all buttons have almost same code as button1 has and here is the code :-

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (exitgame = 0) Then
            If (Button1.Text = "") Then
                Label2.Text = "Active Player Name ==>  " & player
                If (player = "X") Then
                    player = "O"
                Else
                    player = "X"
                End If
                Button1.Text = player
                checkfordraw()
            End If
        End If
End Sub

and here is code for checkfordraw()

Public Sub checkfordraw()
        If (exitgame <> 1) Then
            If (Button1.Text <> "" And Button2.Text <> "" And Button3.Text <> "" And Button4.Text <> "" And Button5.Text <> "" And Button6.Text <> "" And Button7.Text <> "" And Button8.Text And Button9.Text <> "") Then
                If (MsgBox("No more places " & vbNewLine & "Game Draw " & vbNewLine & "would you like to play new game ?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok) Then
                    Button10.PerformClick()
                End If
            End If
        End If
End Sub

when i first click on on any button(except button10) it gives an error saying "InvalidCastException Unhandled" and "Conversion from string "" to type 'Boolean' is not valid".Error occurs at line no three in checkdraw() procedure(i.e second if condition).

Dani AI

Generated

The crash comes from a simple missing comparison in the long If condition: one of the ButtonX.Text terms was left by itself. With Option Strict Off (the default) VB tries to convert that string to a Boolean when it sees an And, which raises the InvalidCastException. correctly spotted the missing test and confirmed it fixed the immediate error.

A safe, minimal fix is to explicitly compare every Text to empty and use AndAlso to short‑circuit:

If exitgame <> 1 Then
    If Not String.IsNullOrEmpty(Button1.Text) AndAlso Not String.IsNullOrEmpty(Button2.Text) AndAlso
       Not String.IsNullOrEmpty(Button3.Text) AndAlso Not String.IsNullOrEmpty(Button4.Text) AndAlso
       Not String.IsNullOrEmpty(Button5.Text) AndAlso Not String.IsNullOrEmpty(Button6.Text) AndAlso
       Not String.IsNullOrEmpty(Button7.Text) AndAlso Not String.IsNullOrEmpty(Button8.Text) AndAlso
       Not String.IsNullOrEmpty(Button9.Text) Then
        ' handle draw
    End If
End If

For maintainability, check the grid with an array (and LINQ) instead of repeating conditions:

Dim cells As Button() = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9}
If cells.All(Function(b) Not String.IsNullOrEmpty(b.Text)) Then
    ' handle draw
End If

Additional recommendations: enable Option Strict On (Project > Properties > Compile or Option Strict On at file top) to catch these mistakes at compile time; prefer AndAlso/OrElse for boolean logic; use String.IsNullOrEmpty rather than <> ""; and consider using a Boolean gameOver flag and a single "ResetGame" routine instead of calling another button from code. These steps prevent the same runtime casting issue and make the logic clearer.

Recommended Answers

All 2 Replies

I guess Button8.Text doesnt have a condition, somewhat like.. Button8.Text<> ""

i didn't notice that.
thanks for pointing it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.