user1392095
user1392095

Reputation:

VB.NET - Sharing variables

I'm trying to make a program for kids to write a letter to Santa, but I've ran into a problem. If I were to use ByVal in a button, I will return an error [Method cannot handle event because they do not have a compatible signature.]

Here's the code...

        Dim formChecked As Boolean
    ' Load complete
    LabelMain.Text = "Write a Letter to Santa!"
    ' Start program
End Sub

Private Sub ButtonCheck_Click(ByVal formchecked As Boolean, ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCheck.Click
    If formChecked = True Then
        ButtonSign.Enabled = False
        ButtonCheck.Text = "Check Letter"
        TextBoxName.Enabled = True
        TextBoxAge.Enabled = True

The top sub declares variables at the start of the program, but I want to be able to share the variable between sub. As you can see in the ButtonCheck_Click sub, I've put in ByVal formchecked as Boolean, but it returns an error of Method cannot handle event because they do not have a compatible signature.

I'm quite a newbie in vb.net, and I was wandering if anyone can help me please.

Upvotes: 2

Views: 253

Answers (1)

Meta-Knight
Meta-Knight

Reputation: 17845

To "share" the variable between methods, make it an instance variable, that is, declare it outside any method, but inside the Form.

Private formChecked As Boolean

Private Sub ButtonCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCheck.Click

    If formChecked = True Then
        ...
    End If

End Sub

Upvotes: 1

Related Questions