Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi, I'm trying to learn an event. However, it can't raise as expected. Here's the code. Please tell me where to change. Thanks in advance.

Public Class Executor

    Public Sub New()
        TryChange()
    End Sub

    Public Sub New(ByVal value As String)
        TryChange(value)
    End Sub

    Public Delegate Sub ExecutorChangeEventHandler(ByVal sender As Object, ByVal e As ExecutorChangeEventArgs)
    Public Event Change As ExecutorChangeEventHandler

    Protected Overridable Sub OnChange(ByVal e As ExecutorChangeEventArgs)
        If Not IsNothing(e) Then
            RaiseEvent Change(Me, e)
        End If
    End Sub

    Public Sub TryChange()
        OnChange(New ExecutorChangeEventArgs)
    End Sub

    Public Sub TryChange(ByVal value As String)
        Dim e As New ExecutorChangeEventArgs
        e.ValueToChange = value
        OnChange(e)
    End Sub

End Class

Public Class ExecutorChangeEventArgs
    Inherits EventArgs

    Protected _values As String = "I LOVE YOU"
    Public Property ValueToChange As String
        Get
            Return _values
        End Get
        Set(ByVal value As String)
            _values = value
        End Set
    End Property


    Public ReadOnly Property Changed As Boolean
        Get
            If ValueToChange <> "I LOVE YOU" Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property

End Class


Here is where I add the event (on button click)
VB
Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim ex As New Executor()
    ex.TryChange()
    AddHandler ex.Change, New Executor.ExecutorChangeEventHandler(AddressOf executor_Change)
End Sub

Private Sub executor_Change(ByVal sender As Object, ByVal e As ExecutorChangeEventArgs)
    MessageBox.Show(e.Changed.ToString)
End Sub


When I click the button, it shows nothing. As you see ther's MessageBox.Show. Help me please.
Posted

1 solution

Hi,

You are trying to handle the event after you have initiated it.

try changing the btn click to this:

VB
Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim ex As New Executor()
    AddHandler ex.Change, New Executor.ExecutorChangeEventHandler(AddressOf executor_Change)
    ex.TryChange()
End Sub


The other issue you have (possibly) is that you are instantiating a new instance of Executor in the button click. By the time the event is called the executor object will not be avaliable to reference back as it was only in the scope of the button click. You should consider moving the ex (Executor) to a private declaration at class level so that it is avalaible for the lifetime of the class object.

i.e.

VB
private ex as Executor

public sub new()
    ex = new Executor
    AddHandler ex.Change, New Executor.ExecutorChangeEventHandler(AddressOf executor_Change)
end sub

Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    ex.TryChange()
End Sub
 
Share this answer
 
Comments
adadero 22-Jul-12 17:06pm    
Hi, it works. Thank you for explaining. Thank you db7uk.
db7uk 22-Jul-12 17:11pm    
no problem. Hope it all goes well from now on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900