PC Review


Reply
Thread Tools Rate Thread

Is it any point to use Onxxx as I have done here when dealing with events and delegates

 
 
Tony Johansson
Guest
Posts: n/a
 
      22nd Feb 2011
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);
}
}
}


 
Reply With Quote
 
 
 
 
Jeff Johnson
Guest
Posts: n/a
 
      22nd Feb 2011
"Tony Johansson" <(E-Mail Removed)> wrote in message
news:ik0sgm$h9r$(E-Mail Removed)...

> 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


The Onxxx method is a much better choice in my opinion. Consider an event
that could be raised in several different ways, it would be better to have
the code in a single, standalone method than in an event handler. It has
long been considered by many people bad form to call an event handler
directly through code.


 
Reply With Quote
 
Willem van Rumpt
Guest
Posts: n/a
 
      22nd Feb 2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
delegates and events dani kotlar Microsoft C# .NET 4 26th Mar 2007 09:31 AM
Custom Events versus Multicast Delegates for events(VB.NET) =?Utf-8?B?aGVyYmVydA==?= Microsoft Dot NET 2 14th Feb 2007 02:51 PM
Advanced use of delegates to automatically disconnect delegates from a set of events kristian.freed@gmail.com Microsoft C# .NET 2 18th Nov 2006 09:56 PM
Should I raise event with Onxxx or without Onxxx tony Microsoft C# .NET 2 16th May 2006 10:16 AM
Dealing with events A+ Computers Windows XP New Users 2 9th Oct 2003 07:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:47 AM.