base class
class Drawer
{
public abstract void Draw<T>(T type);
}
derived class #1
class ADrawer : Drawer
{
public override void Draw<T>(List<T> list)
{
foreach (var a in list)
{
DrawA(a);
}
}
public void DrawA(Agent a)
{
//draw code here
}
}
derived class #2
class AnotherDrawer : Drawer
{
public override void Draw<T>(T number)
{
if (number == 1)
{
//draw code
}
}
}
The error is in the #1 derived class : "no suitable method found to override"
Should I be using 'virtual' in the base class as well as 'abstract' ?
How should I set the base parameter type to allow a variety of parameters in derived classes?
abstract method should have this signeture
To get it to compile change the base class to this:
List<T>
is not the same asT
, so when you pass in aList<T>
in the derived class' method you can't override the base method as that has aT
parameter, not aList<T>
parameter.Your code has more problems than just the one you ask about. Setting aside the override question for the moment, class ADrawer needs a type constraint (
where T : Agent
):Without that constraint, it's not legal to pass
a
toDrawA
, becausea
is a reference of typeT
, and without the constraint there is no implicit conversion from typeT
to typeAgent
.The AnotherDrawer class has an illegal use of the
==
operator. It's not possible to apply the==
operator to operands of typeT
andint
. You could get around that by using theobject.Equals
override.Finally, the base class has an error because it is a non-abstract class containing an abstract member.
In general, however, this code indicates that the class should be generic, rather than the method:
derived class #1
derived class #2
To follow up on Eric Lippert's comment, which was also my first reaction to your question, you might consider this design instead:
derived class #1
Derived class #2 is unchanged.