Link to home
Start Free TrialLog in
Avatar of MattCoatney
MattCoatney

asked on

Delegate Problem: Object Reference not set to an instance of an Object

Requirement:
I am building a custom listbox control and I need to send notification to one control, from another when the Mouse Wheel is used.

Implementation:
I am new to delegates but I thought I had understood them this way:
If Class A needs to tell Class B the mouse wheel was used then...

Class A
protected m_Notify As OnThisMouseWheel

Public Delegate Sub OnThisMouseWheel(Str)

Public Sub NotifyOnMouseWheel(ByVal value As OnThisMouseWheel)
    m_Notify = value
End Sub

Private Sub MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ...

    Try
        m_Notify.Invoke("BOo!")
    Catch ex As Exception

    End Try

End Sub


Class B

Private Sub AddItem(ByVal ob As CustomControl)
    ob.NotifyOnMouseWheel(New CustomControl.OnThisMouseWheel(AddressOf NotifyMe))

    ...
End Sub

Public Sub NotifyMe(ByVal s As String)
    Debug.WriteLine(s)
End Sub


If I invoke m_Notify from NotifyOnMouseWheel in Class A right after assigning it, then it works. The object assignment is working and the delegate communicates to the other control. But after that the m_Notify variable equals Nothing. I Imagine it has something to do with the Subroutine that sets up the controls and creates the delegate but I've tried a couple different ways to pass the variable and I still get the same result. Is this thread related?

It is rather urgent so high points.



Avatar of MattCoatney
MattCoatney

ASKER

To be very clear, the thread question is a subquestion. The Points will be awarded for helping me get the mousewheel event notification to the container control even if that means we don't use delegates.

Thanks in advance.
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
Thanks a bundle.