I have a question about events

Hi, thanks for amazing course, very well explained.

I have a question about events, you have use the EventHandler delegate that could receive some args, why did you choose to pass empty and take the unit selected by an new method instead to pass the “selectedUnit” as the EventArgs?

You can do exactly that. In the beginning of the course, Hugo is introducing the EventHandler, and keeping things simple. Later in the course, he introduces arguments, both as generic parameters and more complex EventArgs child classes.

In this case, declaring the event as an EventHandler would take a delegate of (object, Unit).

1 Like

Yup both options are valid. Like Brian mentioned in the beginning of the course I wanted to keep things simple so I did not want to introduce custom EventArgs right away.

But yup you can definitely make a custom EventArgs that has a Unit
Or alternatively if you want to deviate from the C# standard you could make the event type Unit and pass in the Unit directly.
Or just do like I did, no need to make a custom EventArgs, just use the event to notify that something happened then grab whatever data you need.

All 3 are valid options.

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms