Is it possible to pass Enum elements as event arguments ?
Let's say I have..
public class Letters
{
public delegate void StateChangedEventHandler(object sender, EventArgs e);
public event StateChangedEventHandler StateChanged;
public AbcState state = AbcState.Aaa;
public AbcState State
{
get{return this.state;}
set
{
this.state = value;
this.OnStateChanged();
}
}
public enum AbcState
{
Aaa,
Bbb,
Ccc
}
protected virtual void OnStateChanged()
{
StateChanged?.Invoke(this, State);
}
See how I am trying to pass the element of the enum as an event argument? Now, I normally would create a class and extends EventArgs, and then I would be able to pass the class itself. And I know I could do the same here, Create a different class extending EventArgs and then create the enum, field and property right in the class. And then make an instance of the latter into my class "Letters" (used in code above).
But, isnt that crazy ? There has to be a better way. Please tell me there is a simplier way.
By the way I do not know if the code above would compile I just wrote it straight in the Editor as I am not on my dev computer right now.
Nothing prevents you from not following event handler convention:
And you'll be able to do this:
It's just a convention, not a language limitation.
If you want the parameter of the event to be the enum, then sure, just specify that as the signature when declaring the event:
No, it's not that crazy. It's not at all crazy to follow conventions, even if they seem a little clunky sometimes, because then those who maintain your code (including you, after you forget writing this stuff) will find as few surprises as possible.
You do have the option of using
EventHandler<T>
, however:By the way, you don't have to declare a new redundant delegate type to declare an event. This is the same as your original declaration:
The nicest thing might be to write something like this:
And use it like so:
You could also write a
ValueChangedEventArgs<T>
with old and new value properties, etc. etc. ButEventHandler<AbcState>
is your quickest typesafe way to do what you want to.Finally, you can use
Action<T>
as anevent
delegate, but that's a bad idea. People seeAction<T>
and they don't expect it to be anevent
. The only thing you're adding is confusion when somebody tries to read your code. Unless there is some specific, clear advantage to be had (and there's none here), don't write code which, at first glance, looks like something it isn't.