Edit: I've received some comments identifying this as a duplicate and linking to definitions of the virtual, override, and other such keywords, however that isn't the complete answer that I am looking for. I state below in the original post that I've tried to use the virtual and override keywords but got a compiler error. I originally omitted those keywords from the code because it did not compile but I've put them in now to focus the problem on the fact that the derived class' method signature doesn't match up enough to be able override the base class' method. Simply adding those keywords does not solve my problem. After reading the responses I was given I realized that I need help to make the code compile and achieve the results I'm looking for.
Hi I have code that looks a lot like:
// WeeklyReport.cs
public class WeeklyReport: Report {
// class definitions
}
// ReportService.cs
public class ReportService
{
public async Task<ResponseMessage> ChangeStatus<T>(string reportId, string status) where T:Report, new()
{
var report = await SelectReportById<T>(reportId);
// do stuff
return await SetStatus(change, report);
}
public virtual async Task<ResponseMessage> SetStatus<T>(StatusChange change, T report) where T : Report, new()
{
// do stuff
}
}
// WeeklyReportService.cs
public class WeeklyReportService : ReportService
{
// This method does not compile!!!
public override async Task<ResponseMessage> SetStatus(StatusChange change, WeeklyReport report)
{
// do stuff
}
}
Short description of the above code:
I have two model classes, Report & WeeklyReport. WeeklyReport is a subclass of Report
I have two service classes, ReportService & WeeklyReportService. WeeklyReportService is a subclass of ReportService and is attempting to override its SetStatus() method
Problem:
When I call ChangeStatus() on an instance of WeeklyReportService. I want SetStatus() as defined in WeeklyReportService to be called instead of SetStatus() as defined in ReportService.
I have already tried adding the virtual and override keywords to the method signatures but I get 'WeeklyReportService.SetStatus(StatusChange change, WeeklyReport report)': no suitable method found to override.
Does anyone know how I can achieve this?
Thanks, Ethan