B
B
BadCats2016-08-03 12:50:16
C++ / C#
BadCats, 2016-08-03 12:50:16

Overriding a virtual event?

hello everyone, here is an example:

public delegate void EventDelegate();

    interface IInterface
    {
        event EventDelegate MyEvent; // Абстрактное событие.
    }

    public class BaseClass : IInterface
    {
        EventDelegate myEvent = null;

        public virtual event EventDelegate MyEvent // Виртуальное событие.
        {
            add { myEvent += value; }
            remove { myEvent -= value; }
        }

        public void InvokeEvent()
        {
            myEvent.Invoke();
        }
    }

    public class DerivedClass : BaseClass
    {
        public override event EventDelegate MyEvent // Переопределенное событие.
        {
            add
            {
                base.MyEvent += value;
                Console.WriteLine("К событию базового класса был прикреплен обработчик - {0}", value.Method.Name);
            }
            remove
            {
                base.MyEvent -= value;
                Console.WriteLine("От события базового класса был откреплен обработчик - {0}", value.Method.Name);
            }
        }
    }

    class Program
    {
        // Методы обработчики события.

        static private void Handler1()
        {
            Console.WriteLine("Обработчик события 1");
        }

        static private void Handler2()
        {
            Console.WriteLine("Обработчик события 2");
        }

        static void Main()
        {
            DerivedClass instance = new DerivedClass();

            // Присоединение обработчиков событий.
            instance.MyEvent += new EventDelegate(Handler1);
            instance.MyEvent += new EventDelegate(Handler2);

            // Метод который вызывает событие.
            instance.InvokeEvent();

            Console.WriteLine(new string('-', 20));

            // Открепляем Handler2().
            instance.MyEvent -= new EventDelegate(Handler2);
            instance.InvokeEvent();

            // Delay.
            Console.ReadKey();
        }
    }

For this example, I have three questions:
1) does overriding an event with virtual - just like the InvokeEvent method in this example - allow subclasses to invoke an event by overriding it?
2) If I have one event - is it possible to subscribe this one event to different handlers when redefining it, redefining it in different classes - that is, I mean, if I redefined one virtual event in three different classes and in each of them signed to different event handlers, then if I then create an instance of a particular class, then only handlers from it will work for me, or from all three classes - because I redefined one event in these three classes?
3) I'm also interested in this line:
event EventDelegate MyEvent; // Абстрактное событие.

- as you can see from the comment, the author of the course says that this is an abstract event, but as I understand it, this is not the same as with the abstract keyword? I mean, if in essence it would be the same as with the abstract keyword, then we would have to redefine it, but as you can see, we do not. So, what is meant by the term "abstract event"?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question