June 9, 2007
Hi Guys
I got the following program from the website. Address is given. I couldn’t understand the following code in the program (particularly SomeEvent();????????? it seems a method). Please anybody explain.
public void OnSomeEvent()
{
if(SomeEvent != null)
SomeEvent();
}
Thank you
//A very simple event demonstration
//http://www.java2s.com/Code/CSharp/Language- Basics/Averysimpleeventdemonstration.htm
using System;
// Declare a delegate for an event.
delegate void MyEventHandler(); //Delegate named MyEventHandler
// Declare an event class.
class MyEvent
{
public event MyEventHandler SomeEvent; //an event named SomeEvent
// This is called to fire the event.
public void OnSomeEvent()
{
if(SomeEvent != null)
SomeEvent();
}
}
public class EventDemo
{
// An event handler.
static void handler()
{
Console.WriteLine("Event occurred");
}
public static void Main()
{
MyEvent evt = new MyEvent();
// Add handler() to the event list.
evt.SomeEvent += new MyEventHandler(handler); //SomeEvent - event name
//MyEventHandler - Delegate name
// Fire the event.
evt.OnSomeEvent();
//We need a way to take some actions when an event is fired (or we can say took place)
}
}
/*
Event occurred
*/