473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Should I raise event with Onxxx or without Onxxx

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
May 16 '06 #1
2 1924
"tony" <jo*****************@telia.com> wrote:
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
May 16 '06 #2
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
May 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: alexB | last post by:
You can call the sub() just like any other sub() or function() like so... Call DataGrid1_SelectedIndexChanged(sender,e ) Depending on the circumstances "sender" and "e" may be irrelevent, in...
1
by: moonlight | last post by:
Is event part of C++ language standard? How to implement event in C++? Any advices are welcome!
1
by: Dan Cimpoiesu | last post by:
I have a remoting object, derived from MarshalByRefComponent, that I instantiate on the client side, with Activator.GetObject. Can I receive events fired on the server, on the client? How?
12
by: Vittorio Pavesi | last post by:
Hello, is it possible to manually raise the event Elapsed of the timer object ? Vittorio
12
by: Benny Raymond | last post by:
I understand that you would normally want to raise an event on the same thread so that your code blocks until the event has been dealt with... however i've run into a situation where I have a...
1
by: Anonieko | last post by:
I know Visual Studio lacked support on easily writing code to raise events from a ascx user control ( because you have to hand write them)....
7
by: tony | last post by:
Hello! What is the differens if I use event handler onSizeChanged compare to using the other event handler MeltPracForm_SizeChanged. I see both as event handler is that right? I catch the event...
5
by: Mike | last post by:
Hi group; Let say I have an object called Account, that raises an event called AccountLow with its owns EventArgs, and when this event gets raised, I will like to raise another custom...
2
by: Sin Jeong-hun | last post by:
Suppose class Engine do something in another thread and raise events. class Engine { Thread Worker; public event ... EngineMessage; public void Start() { Worker=new Thread(new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.