When declaring an event such as
public EventHandler<EventArgs> test;
I get the title compiler warning.
with the highlight on “test”
event directly above it is declared exact same syntax and works in the same class.
essentially it will not allow me to declare another event, of EventArgs or any other type in this class. If i comment out the declaration and the raise statement, compiles and executes without issue.
And if I ignore the warning and run, on attempting to raise the event such as
test(this,EventArgs.Empty);
get a null reference exception, what gives, I do not understand how an event ever gets assigned to in the first place?
Your example doesn’t declare an event, it declares a public field that a delegate can be assigned to.
The syntax to make that an event is:
Even with that the event will be null if no one ever registers an event handler so you will have to test that
testis not null before trying to call it.It will be non-null if some other class attached to the event as so:
Take a look at the events tutorial on Msdn.