Edited after initial suggestions about using IEnumerable instead of List.
I am trying to do this:
public interface IKPIDetails // Previously: interface IKPIDetails
{
string name {get; set;}
//...
}
public abstract class BaseReport
{
public List<IKPIDetails> m_KPIDetails; // Previously: list<IKPIDetails> m_KPIDetails;
public BaseReport()
{
m_KPIDetails = MakeReportDataStructure();
//...
}
public abstract List<IKPIDetails> MakeReportDataStructure(); // Previously: public abstract IKPIDetails MakeReportDataStructure();
//...
}
public class AirTemp_KPIDetails : NetVisSolution.Reports.IKPIDetails // Previously: protected class AirTemp_KPIDetails : IKPIDetails
{
#region IKPIDetails implementation
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
#endregion
...
}
public class SSAirTempSummaryReport : BaseReport
{
public SSAirTempSummaryReport() : base ()
{
// Do any extra initialisation here
m_reportName = "Air Temperature Summary Report";
}
//It now complains on the declaration below at the word
// MakeReportDataStructure()
public override IEnumerable<IKPIDetails> MakeReportDataStructure() // Previously: public override List<IKPIDetails> MakeReportDataStructure()
{
List<AirTemp_KPIDetails> d = new List<AirTemp_KPIDetails>();
return d; // <-- this is where it used to break
}
//...
}
So I have a report data object interface and potentially multiple report data classes implementing this interface and adding their specific extra bits. And I have an abstract report class implementing generic report functionality and multiple derived classes that each use their own report data class.
Unfortunately, when I try to create a list of my report detail structures in the derived class and pass it back to the base class as a list of the interface, it complains:
Cannot implicitly convert type
'System.Collections.Generic.List<NetVisSolution.Reports.AirTemp_KPIDetails>' to
'System.Collections.Generic.List<NetVisSolution.Reports.IKPIDetails>'
It won’t let me do it implicitely or explicitly. Is there any way that I can do this?
Thanks in advance,
— Alistair.
you shouldn’t cast a List of one type to another because, then either the insert operation would fail, or the getting.
example: type A implements(extends) type B
if this would legal, then you break the listA invariant by putting something more general than A.
this would break the fact that only A objects, could be assigned to the A reference.