Both of these classes contain another private class that raises events. The two classes then re-raise these events to clients.
Unfortunately, each of the two classes has the same code:
public class FirstClass { public delegate void FooEventHandler(string foo); public delegate void BarEventHandler(string bar); public delegate void BazEventHandler(string baz); public event FooEventHandler Foo; public event BarEventHandler Bar; public event BazEventHandler Baz; private PrivateObject privateObject; public FirstClass() { privateObject.Foo += FirstClass_Foo; privateObject.Bar += FirstClass_Bar; privateObject.Baz += FirstClass_Baz; } private void FirstClass_Foo(string foo) { if (Foo != null) { Foo(foo); } } private void FirstClass_Bar(string bar) { if (Bar != null) { Bar(bar); } } private void FirstClass_Baz(string baz) { if (Baz != null) { Baz(baz); } } }
As you can see, I need to re-create events from a private object. This is redundant. I tried using inheritance and put this repeating code in a base class, but I keep getting errors, like this:
The event "BaseClass.Foo" can be displayed only on the left side of + = or - = (except when it is used inside the type)
Does anyone know how to get rid of this duplicate code?
source share