I have the following class:
public abstract class CommonEvents : DisposableObject
{
/// <summary>
/// Internal method for storing error messages to our event
/// </summary>
/// <param name="message"></param>
/// <param name="showException"></param>
/// <param name="exception"></param>
public void OnError(string message, OnErrorEventsArgs.ShowExceptionLevel showException, Exception exception)
{
if (_onErrorEvent == null) return;
var e = new OnErrorEventsArgs(message, showException, exception);
_onErrorEvent(this, e);
}
/// <summary>
/// Internal event handler allowing for logging of events within the class
/// </summary>
private EventHandler<OnErrorEventsArgs> _onErrorEvent;
/// <summary>
/// Public event handler allowing for accessibility outside of the class
/// </summary>
public event EventHandler<OnErrorEventsArgs> OnErrorEvent
{
add { _onErrorEvent += value; }
remove { _onErrorEvent += value; }
}
}
And I'm trying to inherit it in one of my projects, however I'm already inheriting another class there, the code is below:
public class ItemDal : Common.Dal.BaseDal, CommonEvents
{
The error that I'm receiving is
'CommonEvents': Interface Name expected
When I researched this, I tried to build an interface - but then came up short as I don't understand enough to accomplish this. Can you please help me to understand this or point me in the direction of a good article to help me to do so?
And I'm trying to inherit it in one of my projects, however I'm already inheriting another class there
That's compiler's way of telling you that C# does not support multiple inheritance. If you put a class on the list of bases, the rest of the names must refer to interfaces.
You can inherit two classes only if one of them inherits the other, directly or indirectly; this way you inherit the most derived class, along with the other class that happens to be its base.
A common solution in C# is extracting an interface from one of the classes, embedding the instance of an implementation, and forward interface calls to the implementation included in your class through composition.
In your case it would look like this:
interface ICommonEvents {
void OnError(string message, OnErrorEventsArgs.ShowExceptionLevel showException, Exception exception);
event EventHandler<OnErrorEventsArgs> OnErrorEvent {add;remove;}
}
// Keep the implementation the same
public class CommonEvents : DisposanbleObject, ICommonEvents {
...
}
// Here is your derived class:
public class ItemDal : Common.Dal.BaseDal, ICommonEvents {
private readonly CommonEvents ce = ... // Initialize your common events
void OnError(string message, OnErrorEventsArgs.ShowExceptionLevel showException, Exception exception) {
ce.OnError(message, showException, exception);
}
event EventHandler<OnErrorEventsArgs> OnErrorEvent {
add {ce.OnErrorEvent += value;}
remove {ce.OnErrorEvent -= value;}
}
}
In C#, a class
can only inherit one other class
(though that class may inherit from another and so on).
Anything that follows that one class
after the :
must be an interface, so the compiler is expecting CommonEvents
to be an interface
, but it is not.
There are 2 approaches readily at hand here:
Common.Dal.BaseDal
inherit CommonEvents
(so ItemDal
will inherit CommonEvents
"through" Common.Dal.BaseDal
.ItemDal
. So ItemDal
might expose members similar to the method, but all it does it call into the component, which contains all the logic. This is known as composition.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With