Reputation: 1657
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. :)
Upvotes: 44
Views: 204899
Reputation: 645
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.
Upvotes: 1
Reputation: 1171
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.
Upvotes: 1
Reputation: 466
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;
}
}
}
Upvotes: 3
Reputation: 21
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.
Upvotes: 2
Reputation: 108800
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?
Upvotes: 5
Reputation: 31586
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
Upvotes: 41
Reputation: 298
Make sure that you have the child class explicitly inherit the parent class:
public class Ext : Base { // stuff }
Upvotes: 1
Reputation: 132994
You have forgotten to derive from Base
public class Ext : Base
^^^^^^
Upvotes: 2
Reputation: 103740
You're not inheriting from your base class:
public class Ext : Base {
// constructor
public override void Draw()
{
}
}
Upvotes: 8
Reputation: 1494
Ext needs to inherit the base, so in your definition it should say:
public class Ext : Base { //...
Upvotes: 4