C
C#2y ago
Pacrombie

❔ C# EventArgs

I'm starting to use System.EventHandler events and I'm just wondering if there is a point to the EventArgs class other than to be inherited for custom args? Like, I'm calling StartEvent?.Invoke(this, new EventArgs()); and the new EventArgs() just seems kind of pointless since its empty. Can I send something through the default EventArgs with a lambda expression or something?
44 Replies
canton7
canton72y ago
You can use EventArgs.Empty to avoid an allocation But yeah, it's just there so you can add some args in the future without breaking backwards compat
Pacrombie
PacrombieOP2y ago
And to add those args, you have to make a class deriving from EventArgs?
Thinker
Thinker2y ago
What is the event?
Pacrombie
PacrombieOP2y ago
So
Pacrombie
PacrombieOP2y ago
Pacrombie
PacrombieOP2y ago
I have these guys
Thinker
Thinker2y ago
So you own the event?
Pacrombie
PacrombieOP2y ago
Pacrombie
PacrombieOP2y ago
Yes
Thinker
Thinker2y ago
Really you shouldn't be using just plain EventArgs
Pacrombie
PacrombieOP2y ago
If I don't want to send any args, just use EventArgs.Empty like the other person said? And if I do want to send args, make a class deriving from EventArgs with the values I need?
Thinker
Thinker2y ago
If the event doesn't need any args then the type of the event should be Action
Pacrombie
PacrombieOP2y ago
oh
Thinker
Thinker2y ago
If you need args, you should use Action<T1, T2, ...>
Pacrombie
PacrombieOP2y ago
With Actions, can you still reference the sender in the functions added to it?
Thinker
Thinker2y ago
Well you can add that as an argument
Pacrombie
PacrombieOP2y ago
ah
Pacrombie
PacrombieOP2y ago
Pacrombie
PacrombieOP2y ago
Pacrombie
PacrombieOP2y ago
Like this?
Thinker
Thinker2y ago
What's the point of passing an object as the sender? Do the subscribers actually need to do anything with the sender?
Pacrombie
PacrombieOP2y ago
So a PlayerAbility is invoking these events and I want the UIManager to be able to update with these values with when the event fires. I send the PlayerAbility sender with the event so that the subscribers can get the public values they need. (The UIManager subscribes some of its update functions to the ability events)
Pacrombie
PacrombieOP2y ago
Pacrombie
PacrombieOP2y ago
Would this be a better choice then? Since the sender would only ever be a PlayerAbility?
Thinker
Thinker2y ago
yeah Generally having to cast an EventArgs to the appropriate derived type and having the sender as an object is just bad
Pacrombie
PacrombieOP2y ago
Yeah that's what I've been doing and it just feels bad Thank you for your help :)
canton7
canton72y ago
Eh, the standard advice isn't to use Action etc for events. That's very rare The thing with using EventHandler is that you can add extra args layer without breaking backwards compat. That's why all of Microsoft's events use the event args pattern And yes, so add your own args, make your own EventArgs subclass
Anton
Anton2y ago
If you pass things by ref or in, use a delegate type
Thinker
Thinker2y ago
Although you shouldn't pass just EventArgs
canton7
canton72y ago
Why not? That's why EventHandler and EventArgs.Empty exist, and for exactly the same reason: so that you can add args without a breaking change
Thinker
Thinker2y ago
Well you have to cast them, which is inherently not very safe
canton7
canton72y ago
No you don't?
Thinker
Thinker2y ago
I mean a type deriving from EventArgs sure, not just plain EventArgs
canton7
canton72y ago
I don't get the confusion. If you have an EventHandler event and a subscriber which takes EventArgs, you can update the event to be an EventHander<T> without breaking the subscriber, and later update the subscriber to take that EventArgs subclass No casting required
Thinker
Thinker2y ago
This is of course assuming the subscriber doesn't do anything with the args There's no reason not to use EventHandler<T> from the start
canton7
canton72y ago
EventArgs<T>? That's not a thing What do you mean by that? What would they do with the instance of EventArgs.Empty?
Thinker
Thinker2y ago
I meant EventHsndler<T>
canton7
canton72y ago
EventHandler<EventArgs> is the same as the non generic EventHandler The latter is effectively shorthand for the former
Thinker
Thinker2y ago
EventHandler<YourEventArgs>
canton7
canton72y ago
Yes. But if you don't need any args at the moment, you can just use the base class EventArgs And that's forward compatible for when you do make your own subclass
Thinker
Thinker2y ago
sure although I don't think you should be specifically planning for "when you eventually add arguments"
canton7
canton72y ago
I mean, that's the whole point of the pattern Forwards compatibility For adding some / more new args
Thinker
Thinker2y ago
sure whatever floats your boat
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server