Should I raise event with Onxxx or without Onxxx

T

tony

Hello !!

I have a question about event.
This small program below is taken from the internet and I
wonder if there are any advantage to use this Onxxx in this case it's
OnEventSeven
instead of doing in the following way.

1. In method GenNumbers() you remove this statement "OnEventSeven(new
DivBySevenEventArgs(i));" with these
statements if (EventSeven!=null)
EventSeven(new object(), new
DivBySevenEventArgs(i));


So what is the point of using this Onxx insted of using the event object
directly to raise the event.
Is it perhaps some common practice to use this Onxxx to raise event instead
of using the event object directly.
I feel that this Onxxx is superfluous in some way.


using System;
public delegate void DivBySevenHandler(object o, DivBySevenEventArgs e);
public class DivBySevenEventArgs : EventArgs
{
public readonly int TheNumber;
public DivBySevenEventArgs(int num)
{
TheNumber = num;
}
}

public class DivBySevenListener
{
public void ShowOnScreen(object o, DivBySevenEventArgs e)
{
Console.WriteLine( "divisible by seven event raised!!! the guilty
party is {0}", e.TheNumber);
}
}

public class BusterBoy
{
public static event DivBySevenHandler EventSeven;
public static void Main()
{
DivBySevenListener dbsl = new DivBySevenListener();
EventSeven += new DivBySevenHandler(dbsl.ShowOnScreen);
GenNumbers();
}

public static void OnEventSeven(DivBySevenEventArgs e)
{
if(EventSeven!=null)
EventSeven(new object(),e);
}

public static void GenNumbers()
{
for(int i=0;i<99;i++)
{
if(i%7==0)
{
OnEventSeven(new DivBySevenEventArgs(i));
}
}
}
}

//Tony
 
B

Barry Kelly

tony said:
So what is the point of using this Onxx insted of using the event object
directly to raise the event.

The standard pattern is that every event X has a protected virtual
method called OnX which invokes the event, and whenever the class wishes
to raise the X event it calls the OnX method.

The reason this is done is to allow descendant classes to receive
notification of the event (1) without having to hook into the X event
and (2) to prevent raising of the event if that is desired.

In your example, the OnX method was static, and so was superfluous.

-- Barry
 
M

Marc Gravell

The OnSomeEvent pattern has many things in its favor:
* If the same event is called by multiple code-blocks in the class, then
they are all consistent
* It allows a centralised maintenance point - e.g. suppose that the event
was tweaked to have a custom (private) delegate underneath it, e.g. to
support different locking or serialization rules - if you have used the
event directly you need to update each calling code. Similarly, the new
object() as the sender may (for all I know) be a bug (looks unusual, at
least); a pain to update if scattered, but easy if central
* It allows the OnSomeEvent to be made protected (or even public) in a
structured manner - where-as a vanilla event can only be fired by the owning
class (and not sub-classes); virtual (polymorphism) is quite a common touch
as well.
* Just general encapsulation things; for instance, the OnSomeEvent method
might not have the sender as an argument (since it can juts use "this"
inside), and may not accept the actual event-args type - just the data
needed to /build/ the event-args object - makes calling it much cleaner.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top