I have an interface defined like so:
public interface IRipper { IRipperHost Host { get; set; } void Rip(FileStream stream); event RipperEventHandler OnStart; event RipperEventHandler OnComplete; event RipperEventHandler OnProgressChanged; } public delegate void RipperEventHandler(object sender, RipperEventArgs e);
and a base class that implements that interface
public class RipperBase : IRipper { public void Rip(FileStream stream) { throw new System.NotImplementedException(); } public event RipperEventHandler PostRipEvent; event RipperEventHandler IRipper.OnComplete { add { if (PostRipEvent != null) { lock (PostRipEvent) { PostRipEvent += value; } } else { PostRipEvent = value; } } remove { if (PostRipEvent != null) { lock (PostRipEvent) { PostRipEvent -= value; } } } } ... and so on }
My question is how would you inherit from RipperBase and override the BaseClass Rip method. in reality that function should be abstract – but i don’t know how to do that with interfaces.
Also how can i call the base classes event handler from the class derived from RipperBase? I tried this
RipperEventHandler handler = PreRipEvent; if (handler != null) { handler(this, new RipperEventArgs { Message = 'Started Ripping file to CSV!' }); } handler = RipProgEvent;
this but I’m getting the ‘event can only appear on the left hand side of += or -=.’ error
any help would be greatly appreciated! I’m trying to make this as scalable as possible.
I would do it like this:
I’ve declared the class as abstract, since I presume that you cannot instantiate objects from RipperBase ?
Classes that inherit from RipperBase have to override the RipCore method, and implement the correct functionality. Rip is the method that calls the RipCore method, and it also raises the Completed eventhandler. (Maybe you do not want to do this, i dunno).
You can also make sure that the Rip method calls the RipCore method on another thread if you want.
For the events, I create a protected method in the base class that can be used to raise the events. If you want to raise the event in a subclass, you just call this method.