Thanks
tommmyyy123
Posts
-
event in c# -
event in c#And how I test the null handler list in Main : if (evt.SomeEvent == null) Console.WriteLine(“Event refered the null list”);
-
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