I am trying to learn C# and have up until this point worked a lot with Java.
Now I have several times already met this problem and it is starting to annoy me a bit so for the sake of example lets look at some code.
Say for instance I have the following abstract
class:
public abstract class ChartHelper
{
public System.Windows.Forms.DataVisualization.Charting.Chart resultChart { get; set; }
public String TimeType { get; set; }
protected List<IObject> _datalist;
protected TimeType _timeType;
protected DateTime _stopDate;
protected DateTime _startDate;
}
Now as you can see i have a List<>
that will contain elements of the interface IObject
:
public interface IObject
{
DateTime GetPeriod();
}
Now an element that implements this interface could be the following:
public class Email : IObject
{
public virtual DateTime PERIOD { get; set; }
public virtual int email_u_kam { get; set; }
public virtual int kam_ialt { get; set; }
public virtual int ms_besvarelses_pc { get; set; }
public virtual int total_besvarelses_pc { get; set; }
public DateTime GetPeriod()
{
return PERIOD;
}
}
Now for an extension of the ChartHelper
class I would like to create an Email
chart constructor and for this purpose I of course extend
the ChartHelper
class:
public class EmailChartGenerator : ChartHelper, IChart
{
public EmailChartGenerator(List<Email> dataList, TimeType timeType, DateTime startDate, DateTime stopDate) : base()
{
_startDate = startDate;
_stopDate = stopDate;
_datalist = dataList;
_timeType = timeType;
}
}
Notice that in the constructor I specify the list as the type Email
since this generator will only be used for creating charts that display Email
data.
And now to the problem. because the protected
field of the abstract class ChartHelper
_datalist is specified as IObject
I get the following error in my generator's constructor:
Cannot implicitly convert type 'System.Collections.Generic.List<Henvendelser.Objects.Email>' to 'System.Collections.Generic.List<Henvendelser.Objects.IObject>'
even though the Email
object is implementing the interface IObject
. I know this was kind of a long post but I needed to make sure I had all the details! Why is this happening and shouldn't Email
be equal to IObject
?