event in c#
-
Hi, I have the code:
using System;
// Declare a delegate type for an event.
delegate void MyEventHandler();
// Declare a class that contains an event.
class MyEvent
{
public event MyEventHandler SomeEvent;
// This is called to raise the event.
public void OnSomeEvent()
{
if (SomeEvent != null)
SomeEvent();
}
}
class EventDemo
{
// An event handler.
static void Handler()
{
Console.WriteLine(“Event occurred”);
}
static void Main()
{
MyEvent evt = new MyEvent();
// Add Handler() to the event list.
evt.SomeEvent += Handler;
if (evt.SomeEvent == null) Console.WriteLine(“Ev. nu are nici o metoda in lista”);
// Raise the event.
evt.OnSomeEvent();
}
}Why in the case of the second if (from Main) I have compiler error: error CS0070: The event ‘ConsoleApplication2.MyEvent.SomeEvent’ can only appear on the left hand side of += or -= (except when used from within the type ‘ConsoleApplication2.MyEvent’)
And in the case of te first if it is OK ?
tom
-
Hi, I have the code:
using System;
// Declare a delegate type for an event.
delegate void MyEventHandler();
// Declare a class that contains an event.
class MyEvent
{
public event MyEventHandler SomeEvent;
// This is called to raise the event.
public void OnSomeEvent()
{
if (SomeEvent != null)
SomeEvent();
}
}
class EventDemo
{
// An event handler.
static void Handler()
{
Console.WriteLine(“Event occurred”);
}
static void Main()
{
MyEvent evt = new MyEvent();
// Add Handler() to the event list.
evt.SomeEvent += Handler;
if (evt.SomeEvent == null) Console.WriteLine(“Ev. nu are nici o metoda in lista”);
// Raise the event.
evt.OnSomeEvent();
}
}Why in the case of the second if (from Main) I have compiler error: error CS0070: The event ‘ConsoleApplication2.MyEvent.SomeEvent’ can only appear on the left hand side of += or -= (except when used from within the type ‘ConsoleApplication2.MyEvent’)
And in the case of te first if it is OK ?
tom
Because that's how events work in C#. :-D Only the class that defines the event can access it that way; other classes can only add and remove handlers. However, a common practice with events is for the class to declare a Raise method that will check for null and raise the event. Ideally this method would be private or protected, but you could make it public if you really wanted to. If you are just learning about events that may make some sense, but it's generally not a good idea as it pretty much violates the whole idea of events. Furthermore, if that does make sense, maybe you just want a regular delegate rather than an event. We would need to know more about what you are trying to accomplish.
-
Because that's how events work in C#. :-D Only the class that defines the event can access it that way; other classes can only add and remove handlers. However, a common practice with events is for the class to declare a Raise method that will check for null and raise the event. Ideally this method would be private or protected, but you could make it public if you really wanted to. If you are just learning about events that may make some sense, but it's generally not a good idea as it pretty much violates the whole idea of events. Furthermore, if that does make sense, maybe you just want a regular delegate rather than an event. We would need to know more about what you are trying to accomplish.
And how I test the null handler list in Main : if (evt.SomeEvent == null) Console.WriteLine(“Event refered the null list”);
-
And how I test the null handler list in Main : if (evt.SomeEvent == null) Console.WriteLine(“Event refered the null list”);
you don't. think what you are trying to do here, you have a class called MyEvent which owns and exposes an event. That class is solely responsible for managing that event, other classes interacting with it are merely listeners The only point at which an event being null should matter is if you are about to invoke it. The only thing that invokes an event is its owner class and therefore only MyEvent needs to care about SomeEvent being null. Therefore if you want a console output for an attempt to invoke an event for which no event handler has been attached it should go in the OnSomeEvent method of the MyEvent class
Pedis ex oris Quidquid latine dictum sit, altum sonatur
-
And how I test the null handler list in Main : if (evt.SomeEvent == null) Console.WriteLine(“Event refered the null list”);
You shouldn't, so you can't directly, at best the class can allow you to do so indirectly, but that's not how events are meant to work.
-
You shouldn't, so you can't directly, at best the class can allow you to do so indirectly, but that's not how events are meant to work.
Thanks