Jeffrey L. Roberts
Jeffrey L. Roberts

Reputation: 3004

Cascading Multiple Inheritance and Events

I really am not sure what else to google to try to find the answer to this, can anyone tell me the proper way to implement these events? In ClassOne the event is considered null, and I just don't get it ....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MultipleInheritance
{
    static class Program
    {
        static void Main()
        {
            ClassThree cThree = new ClassThree();
            cThree.fireEventOne += cThree_fireEventOne;
            cThree.Start();
            cThree.Start2();
            cThree.Start3();
        }

        static void cThree_fireEventOne()
        {
            Console.WriteLine("one two three");
        }
    }
}

and here is the classes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MultipleInheritance
{
    public abstract class ClassOne
    {
        public delegate void EventOne();
        public event EventOne fireEventOne;

        public ClassOne()
        {

        }

        public void Start()
        {
            fireEventOne();
        }

        public abstract void Start2();
    }

    public abstract class ClassTwo :ClassOne
    {
        public delegate void EventOne();
        public event EventOne fireEventOne;

        public override void Start2()
        {
            fireEventOne();
        }

        public abstract void Start3();
    }

    public class ClassThree :ClassTwo
    {
        public delegate void EventOne();
        public event EventOne fireEventOne;

        public override void Start3()
        {
            fireEventOne();
        }
    }
}

Upvotes: 0

Views: 652

Answers (2)

Enigmativity
Enigmativity

Reputation: 117175

The problem is that you're redefining the following in each class:

public delegate void EventOne();
public event EventOne fireEventOne;

So when you call Start() you are trying to fire the event fireEventOne in the class ClassOne, but you've hooked up the shadowed event in ClassThree like this:

cThree.fireEventOne += cThree_fireEventOne;

I can see why you coded it that way. You get a compiler error when trying to directly invoke the event from a parent class. The correct way is to provide a protected method that you call in the parent class to fire the event.

Try writing your classes like this:

public abstract class ClassOne
{
    public delegate void EventOne();
    public event EventOne fireEventOne;

    public ClassOne()
    { }

    public void Start()
    {
        this.DoFireEventOne();
    }

    protected void DoFireEventOne()
    {
        var feo = fireEventOne;
        if (feo != null)
        {
            feo();
        }
    }

    public abstract void Start2();
}

public abstract class ClassTwo :ClassOne
{
    public override void Start2()
    {
        this.DoFireEventOne();
    }

    public abstract void Start3();
}

public class ClassThree :ClassTwo
{
    public override void Start3()
    {
        this.DoFireEventOne();
    }
}

Upvotes: 3

Gosha_Fighten
Gosha_Fighten

Reputation: 3868

You create the fireEventOne event in each class. So, you have three events. But, the fireEventOne method is implemented on the first level (ClassOne). So, it will raise the delegate of the ClassOne class. Remove the fireEventOne event for the descendant classes.

Upvotes: 1

Related Questions