Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event default initializer

I used to utilize the following:

public event EventHandler OnComplete = delegate { };

I'm not sure, how this is called, is this an "event default initializer"??

But the problem appeared to be when I derived from EventArgs, created my own EventHandler and decided to use the same approach. Please, see:

public class MyEventArgs : EventArgs
{
    int result;
    public int Result
    {
        get
        {
            if (exceptionObject == null)
                return result;
            else
                throw new InvalidOperationException();
        }
        internal set { result = value; }
    }

    Exception exceptionObject;
    public Exception ExceptionObject
    {
        get { return exceptionObject; }
        internal set { exceptionObject = value; }
    }
}

public delegate EventHandler MyEventHandler(object sender, MyEventArgs e);

public class MyOperation
{
    public event MyEventHandler OnOperationComplete = delegate { };
}

So, the line

public event MyEventHandler OnOperationComplete = delegate { };

causes the problem.

How can I make the proper default initialization for "my" events?

like image 359
horgh Avatar asked Nov 29 '25 04:11

horgh


1 Answers

public event MyEventHandler OnOperationComplete = (sender, args) => { return null; };

I also think you meant to say:

public delegate void MyEventHandler(object sender, MyEventArgs e);

not

public delegate EventHandler MyEventHandler(object sender, MyEventArgs e);
like image 193
THX-1138 Avatar answered Nov 30 '25 17:11

THX-1138



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!