简体   繁体   中英

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. :)

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? Ie 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

You're not inheriting from your base class:

public class Ext : Base {
    // constructor

    public override void Draw()
    {
    }
}

The signature of your methods is different. But to override a method the signature must be identical.

In your case the base class version has no parameters, the derived version has one parameter.

So what you're trying to do doesn't make much sense. The purpose is that if somebody calls the base function on a variable that has the static type Base but the type Ext at runtime the call will run the Ext version. That's obviously not possible with different parameter counts.

Perhaps you don't want to override at all?

I ran into this issue only to discover a disconnect in one of my library objects. For some reason the project was copying the dll from the old path and not from my development path with the changes. Keep an eye on what dll's are being copied when you compile.

Ext需要继承base,所以在你的定义中它应该说:

public class Ext : Base { //...

You cannot override a function with different parameters, only you are allowed to change the functionality of the overridden method.

//Compiler Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

//Overriding & overloading

namespace polymorpism
{
    public class Program
    {
        public static void Main(string[] args)
        {

            drowButn calobj = new drowButn();

            BtnOverride calobjOvrid = new BtnOverride();

            Console.WriteLine(calobj.btn(5.2, 6.6).ToString());

            //btn has compleately overrided inside this calobjOvrid object
            Console.WriteLine(calobjOvrid.btn(5.2, 6.6).ToString());
            //the overloaded function
            Console.WriteLine(calobjOvrid.btn(new double[] { 5.2, 6.6 }).ToString());

            Console.ReadKey();
        }
    }

    public class drowButn
    {

        //same add function overloading to add double type field inputs
        public virtual double btn(double num1, double num2)
        {

            return (num1 + num2);

        }


    }

    public class BtnOverride : drowButn
    {

        //same add function overrided and change its functionality 
        //(this will compleately replace the base class function
        public override double btn(double num1, double num2)
        {
            //do compleatly diffarant function then the base class
            return (num1 * num2);

        }

        //same function overloaded (no override keyword used) 
        // this will not effect the base class function

        public double btn(double[] num)
        {
            double cal = 0;

            foreach (double elmnt in num)
            {

                cal += elmnt;
            }
            return cal;

        }
    }
}

You have forgotten to derive from Base

public class Ext : Base
                 ^^^^^^        

我没有看到类Ext是从Base派生的。

您需要从基类继承。

确保您让子类显式继承父类:

public class Ext : Base { // stuff }

I ran into a similar situation with code that WAS working , then was not.

Turned while dragging / dropping code within a file, I moved an object into another set of braces. Took longer to figure out than I care to admit.

Bit once I move the code back into its proper place, the error resolved.

Probably your base class is a different one with the same name. Check by right click on the extended class and go to the definition. Check if the class is correct one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM