Using a delegate to send a string

I have a very annoying problem in my code when I try to submit a string from form B to form a. I am getting the error:

Object reference not set to an instance of an object.

      

I am familiar with this error and usually know how to fix this problem, but this is different.

I need to submit Clockname from one form to the main form, I am trying to achieve this using the following code:

delegate void ClockClocknameReceivedEventHandler(object sender, Clock.ClocknameReceivedEventArgs e);

    internal class ClocknameReceivedEventArgs : EventArgs
    {
        string _clockname;

        public string Clockname
        {
            get { return _clockname; }
        }

        public ClocknameReceivedEventArgs(string clockname)
        {
            _clockname = clockname;
        }
    }

    // An event that clients can use to be notified whenever the
    // elements of the list change.
    public event ClockClocknameReceivedEventHandler ClocknameReceived;

    // Invoke the Changed event; called whenever list changes
    protected void OnClocknameReceived(Clock.ClocknameReceivedEventArgs e)
    {
        ClocknameReceived(this, e);
    }

      

When the button is clicked, the following code is run, after which the form is closed:

OnClocknameReceived(new Clock.ClocknameReceivedEventArgs(ClockName));

      

Error (Object reference not set to object instance.) I get the message in

ClocknameReceived(this, e);

      

I am using the exact same code from another class to the main form to submit the byte array which works great but it gives me this error.

Any ideas?

Thanks in advance!

+3


source to share


3 answers


The delegate can be null

. Call it only if it is not null

:

protected void OnClocknameReceived(Clock.ClocknameReceivedEventArgs e)
{
    ClockClocknameReceivedEventHandler handler = ClocknameReceived;
    if (handler != null)
    {
        handler(this, e);
    }
}

      


A delegate null

when you haven't yet subscribed an event handler. Sign event handler:



formB.ClocknameReceived += FormB_ClocknameReceived;

      

from

void FormB_ClocknameReceived(object sender, Clock.ClocknameReceivedEventArgs e)
{
    MessageBox.Show(e.Clockname);
}

      

+4


source


It does not check if the event has been assigned ClocknameReceived

(i.e. there are any subscribers). Typical event handling code usually looks like this:

var handler = ClocknameReceived;
if (handler != null)
{
    handler(this, e);
}

      

This type of approach also mitigates (to some extent) the race conditions with your event handler, as it might be unassigned by the time it runs.

By looking at your code, you can definitely tweak it a bit. For example, your class EventArgs

could be rewritten with less code:

internal class ClocknameEventArgs : EventArgs
{
    public ClockNameEventArgs(string name)
    {
        Name = name;
    }

    public string Name { get; private set; }
}

      



Then there is no need for delegation in your form, if you have a specific type EventArgs

, your event can be declared as:

public event EventHandler<Clock.ClocknameEventArgs> ClocknameReceived;

      

Then you hook up to this event somewhere (perhaps in an event FormCreate

?):

ClocknameReceived += (sender, args) {
    FormB.PassName(args.Name);
};

      

+1


source


You have to check if the event has a delegate or not

if( ClocknameReceived != null)
    ClocknameReceived(this, e);

      

0


source







All Articles