?
Solved

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

Posted on 2006-05-16
3
Medium Priority
?
279 Views
Last Modified: 2012-06-27
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.



0
Comment
Question by:MattCoatney
  • 2
3 Comments
 

Author Comment

by:MattCoatney
ID: 16692295
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.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 16693597
How about...

Public Class myListBox
    Inherits ListBox

    Protected m_Notify As OnThisMouseWheel

    Public Delegate Sub OnThisMouseWheel(ByVal Str As String)

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

    Protected Overrides Sub OnMouseWheel(ByVal e As System.Windows.Forms.MouseEventArgs)
        If Not (m_Notify Is Nothing) Then
            m_Notify("Boo!")
        End If
    End Sub

End Class


Public Class myListBoxSubscriber

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

End Class


Public Class Form1
    Inherits System.Windows.Forms.Form

    Private mlbs As myListBoxSubscriber

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mlb As New myListBox
        mlb.Location = New Point(0, 0)
        mlb.Size = New Size(50, 200)
        For i As Integer = 1 To 100
            mlb.Items.Add("Item" & i)
        Next
        Me.Controls.Add(mlb)

        mlbs = New myListBoxSubscriber
        mlb.NotifyOnMouseWheel(New myListBox.OnThisMouseWheel(AddressOf mlbs.NotifyMe))
    End Sub

End Class
0
 

Author Comment

by:MattCoatney
ID: 16700476
Thanks a bundle.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question