Jamie Jamie - 2 months ago 8
C# Question

C# "No suitable method found to override." -- but there is one

I'm having trouble overriding the method of a parent class in C#. The parent class is setup like so:

public class Base {
public Base(Game1 game)
{
this.game = game;
}

public virtual void Draw()
{
}
}


...And the child class:

public class Ext : Base {
public Ext(Game1 game) : base(game)
{
}

public override void Draw(SpriteBatch batch)
{
}
}


I know I've successfully overridden a parent method in the past, and right now I'm probably overlooking something incredibly simple... what is it?

EDIT: That was actually a typo: in the actual script, Ext does derive from Base. The problem still persists. Thanks for the quick answers, though. :)

Answer

Your code as given (after the edit) compiles fine, so something else is wrong that isn't in what you posted.

Some things to check, is everything public? That includes both the class and the method.

Overload with different parameters?

Are you sure that Base is the class you think it is? I.e. is there another class by the same name that it's actually referencing?

Edit:

To answer the question in your comment, you can't override a method with different parameters, nor is there a need to. You can create a new method (with the new parameter) without the override keyword and it will work just fine.

If your intention is to prohibit calling of the base method without the parameter you can mark the method as protected instead of public. That way it can only be called from classes that inherit from Base

Comments