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