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
{
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
*/
AlanPosted Sep 15, 2008, 2:53 PM
Yes, it will be null if no eventhandlers have been attached to it using the '+=' operator. You can see this if you make a couple of changes to the program (marked in red):
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();
else
Console.WriteLine("Event is null");
}
}
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); // commented out
//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)
}
}
Posted Sep 15, 2008, 3:40 PM
Posted Sep 15, 2008, 11:20 AM
Is there any occasion in which SomeEvent can be null.
AlanPosted Jun 9, 2007, 7:53 AM
The OnSomeEvent() method is the method which code outside the MyEvent class needs to call to 'fire' the SomeEvent event.
This line:
if(SomeEvent != null)
SomeEvent();
checks to make sure that SomeEvent, which as well as being an event is also a delegate of type MyEventHandler, has been set equal to an instance of that delegate. If it has, it then calls the SomeEvent() event/delegate. This means that it calls all the methods in the invocation list of the delegate one after the other.
In the EventDemo class, SomeEvent has been initialized, using the line:
evt.SomeEvent += new MyEventHandler(handler);
by setting it equal to a new instance of the MyEventHandler delegate and placing the method 'handler' in first position in its invocation list.
The event is then fired with the line:
evt.OnSomeEvent();
As the SomeEvent delegate is not null because it was initialized earlier, all methods in its invocation list are called. In this case, there is just one method in the invocation list, namely the static method handler(), which is therefore called and prints "Event occurred" to the console.