Reputation: 441
Im trying to write abstract class for different reports.
I have a method
protected Tuple<byte[], string, string> RenderReport()
which has such lines
var localReport = new LocalReport { ReportPath = _reportLocalFullName };
...
localReport.SubreportProcessing += localReport_SubreportProcessing;
Derived class must write own code in the localReport_SubreportProcessing.
I'm not sure how to make inheritance here. Can someone help ?
Upvotes: 0
Views: 84
Reputation: 4352
Try like below.
public abstract class BaseReport
{
......
protected Tuple<byte[], string, string> RenderReport()
{
var localReport = new LocalReport { ReportPath = _reportLocalFullName };
...
localReport.SubreportProcessing += localReport_SubreportProcessing;
...
}
protected abstract void LocalReport_SubreportProcessing(object sender, EventArgs e);
}
public class DerivedReport1 : BaseReport
{
protected override void LocalReport_SubreportProcessing(object sender, EventArgs e)
{
// Report generation logic for report1.
}
}
public class DerivedReport2 : BaseReport
{
protected override void LocalReport_SubreportProcessing(object sender, EventArgs e)
{
// Report generation logic for report2.
}
}
Upvotes: 0
Reputation: 156918
You can call a common method, which you override in your base
class.
So in localReport_SubreportProcessing
, call ProcessSubreport
private void localReport_SubreportProcessing(object sender, EventArgs e)
{
this.ProcessSubreport();
}
protected virtual void ProcessSubreport()
{ }
And override it in your deriving class:
protected override void ProcessSubreport()
{ }
Upvotes: 1
Reputation: 1062502
Rather than having a method:
private void localReport_SubreportProcessing(...) {...}
consider instead:
protected virtual void OnSubreportProcessing(...) {...}
Now your subclasses can simply use:
protected override void OnSubreportProcessing(...) {...}
Upvotes: 5