Link to home
Start Free TrialLog in
Avatar of Jess31
Jess31

asked on

Variable Event ?

Using vb.net and winforms.
In the code behind the Form - how can I set an event on a local variable that will trigger anytime the variable changes value?
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

You can't for a variable.  It doesn't work that way.
Use a class:
Public Class MyVariable

    Public Event Changed()
    Private _Data As String
    Property Data As String
        Get
            Data = _Data
        End Get
        Set(value As String)
            _Data = value
            RaiseEvent Changed()
        End Set
    End Property

End Class

Open in new window


and in your form/module:
Private WithEvents testVar As New MyVariable

    Private Sub varChanged() Handles testVar.Changed
        'do your thing here
        MessageBox.Show("You variable has changed")
    End Sub

    Private Sub btnTest_Click(sender As System.Object, e As System.EventArgs) Handles btnTest.Click
        testVar.Data = "testdata"
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial