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.Mouse EventArgs) 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.OnThisMouseW heel(Addre ssOf 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.
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.Mouse
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.OnThisMouseW
...
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a bundle.
ASKER
Thanks in advance.