On 22-Feb-11 18:41, Tony Johansson wrote:
> Hello!
>
>
> Here is an example of some code(WPF) where I use On concatenated with the
> event .
> I have seen this before but I just wonder is it any point to do it in this
> way.
> I mean I can just the same move the code within this On into the event
> handler for the button
>
> public delegate void SignToHandleTakeOff(object sender, TakeOffEventArgs
> args);
> public delegate void SignToHandleChangeRoute(object sender,
> ChangeRouteEventArgs args);
> public delegate void SignToHandleLand(object sender, LandEventArgs args);
>
> public partial class FlightForm : Window
> {
> public event SignToHandleTakeOff takeOff;
> public event SignToHandleChangeRoute changeRoute;
> public event SignToHandleLand land;
> private string flightCode;
> const string TakeOff = "started";
> const string Land = "landed";
>
> public FlightForm(string flightCode)
> {
> InitializeComponent();
> LoadComboBox();
> this.flightCode = flightCode;
> }
>
> private void LoadComboBox()
> {
> foreach (string route in
> Common.GetAirplaneActionsAcceptStartAndLand())
> {
> cboChangeRoute.Items.Add(route);
> }
> cboChangeRoute.SelectedIndex = 0;
> }
>
> public void OnTakeOff()
> {
> SignToHandleTakeOff handler = takeOff;
> if (handler != null)
> handler(this, new TakeOffEventArgs(TakeOff, flightCode,
> DateTime.Now));
> }
>
> private void BtnTakeOff_Click(object sender, RoutedEventArgs e)
> {
> OnTakeOff();
> }
>
> private void OnLand()
> {
> SignToHandleLand handler = land;
> if (handler != null)
> handler(this, new LandEventArgs(Land, flightCode,
> DateTime.Now));
> }
>
> private void BtnLand_Click(object sender, RoutedEventArgs e)
> {
> OnLand();
> }
>
> private void OnChangeRoute(string thisRoute)
> {
> SignToHandleChangeRoute handler = changeRoute;
> if (handler != null)
> handler(this, new ChangeRouteEventArgs(thisRoute,
> flightCode, DateTime.Now));
> }
>
> private void CboChangeRoute_SelectionChanged(object sender,
> SelectionChangedEventArgs e)
> {
> OnChangeRoute(cboChangeRoute.Text);
> }
> }
> }
>
>
Name wise (regarding your question), it's mostly a convention: Methods
handling the invoking of a event handler are named "On...Action".
Implementation wise, the reason to have them around is that there might
be multiple locations from which to invoke an event handler. The methods
are almost always implemented either as private, or as
protected/internal/internal protected methods (in those latter cases
they would most likely also be virtual) and serve the same purpose: Do
some bookkeeping, validate whether the event was assigned to, possibly
create some EventArgs descendant, and, finally invoke the event (your
OnChangeRoute is an example).
The reason they are given their own method, is to separate the idea of
doing something, and notifying something. Taking off is one task,
notifying something of taking off taking place is a different one
(although they might interfere, of course

).
--
Willem van Rumpt