none
Why is my Event not being consumed RRS feed

  • Question

  • I have a class defined as follows:

    Public Class Utility Implements INotifyPropertyChanged Private clsActive As Boolean <other properties> Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Public Property Active() As Boolean Get Return clsActive End Get Set(ByVal value As Boolean) If clsActive <> value Then clsActive = value

    OnPropertyChanged("Active") End Set End Property <Other Properties> Protected Sub OnPropertyChanged(Name As String) Update() RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Name)) End Sub End Class


    and a handler as follows:

    Friend Sub UpdateUtility(s As Object, e As PropertyChangedEventArgs) Handles u.PropertyChanged
    		Debug.WriteLine(e.PropertyName)
    	End Sub

    The Utility class is instantiated as follows:

    Friend WithEvents u As Utility
    When a Utility property changes I  know the event is raised because the Update routine is being completed successfully.  But the Handler is never invoked because the Debug statement never is reached.  Why?



    • Edited by SezMe Saturday, September 29, 2018 9:15 PM
    Saturday, September 29, 2018 9:10 PM

All replies

  • Are you running in Debug mode (versus Release)?

    Also, when you put a break point on the UpdateUtility(), is it hit?


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Saturday, September 29, 2018 9:44 PM
    Moderator
  • Yes.

    No.

    Saturday, September 29, 2018 11:45 PM
  • Make sure that the Utility object was assigned to u before invoking the events.


    Sunday, September 30, 2018 6:42 AM
  • It has to have been instantiated since, as I mentioned, the Update routine executes properly.
    Sunday, September 30, 2018 8:58 AM
  • You've not shown all the relevant code so people can only guess what you have going on.


    My best guess is that you have declared 2 'u' variables: one at class level as you have shown (that is the one associated with the event handler), and one at method level which isn't associtaed with theevent handler.


    Something like:

    Friend WithEvents u As Utility
    
    Private Sub Button1_Click(sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim u = New Utility
        u.Active = Not u.Active
    
    End Sub
    
    Friend Sub UpdateUtility(s As Object, e As PropertyChangedEventArgs) Handles u.PropertyChanged
        Debug.WriteLine(e.PropertyName)
    End Sub
    
    The   Dim u = New Utility  in the above example should just be   u = New Utility


    Sunday, September 30, 2018 10:23 AM
  • I rebuilt my whole project, not changing any code but rather, putting the various elements together in a different manner.  That has solved the problem although I must confess I don't understand why.

    In any case, thanks for the assistance.

    Sunday, September 30, 2018 9:03 PM