What a great site this is, I have lurked on here reading others questions for ages but now I have one of my own.
My workmate wrote a class very like the one below. As soon as I saw it I knew it wouldn't work but I have no explination for him why it doesn't work.
What he expected when declaring it as a ControlItem<Button>
is that the Draw(Button) method would be called when using the base to call Draw(). Instead we always end up throwing the exception.
Is this a covariance issue?
public abstract class ControlItem
{
public ControlItem()
{
}
abstract public void Draw();
}
public class ControlItem<T> : ControlItem where T : Control, new()
{
public T MyControl { get; set; }
private ControlItem()
{ }
public ControlItem(T control)
: base()
{
MyControl = control;
}
public override void Draw()
{
Draw(this.MyControl);
}
public void Draw(Control cntrl)
{
throw new NotImplementedException();
}
public void Draw(Button button)
{
//Do some work
}
}