Al6200

Warning 2 'WindowsApplication3.generalEnemy.onEnterFrame()' hides inherited member 'WindowsApplication3.SceneObjects.onEnterFrame()'. Use the new keyword if hiding was intended. C:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\WindowsApplication3\WindowsApplication3\generalEnemy.cs 24 21 WindowsApplication3

is a warning I'm getting.

Basically I have an abstract superclass SceneObjects, and an abstract subclass Characters, which has an abstract subclass generalEnemy.

There is a method called onEnterFrame which is defined as public void in all of those classes. I can't define it as abstract in these classes because some of the subclasses are abstract, and therefore I DON'T want them to implement it.

I can't remove onEnterFrame() from my abstract classes because there is a point where I am using polymorphism (overriding) like superclass a = new subclass(). But when I define onEnterFrame() as public in my abstract superclasses, I get an error that my subclasses are trying to "hide" the superclasses's method.

It tells me to "use the the new keyword" if hiding was intended. Can someone explain how the new keyword is pertinent, and what exactly hiding is Where I'm from, overriding a superclasses's method is called inheritance.



Re: Visual C# Language I keep getting a hiding error. I'm confused.

John.Doe

How about you declare it as "virtual" and just override it if you want to



Re: Visual C# Language I keep getting a hiding error. I'm confused.

Marcelo Guerra - MSFT

Hi,

In C# you need to explicitly specify that you are overriding something using the virtual keyword on the superclass (abstract makes a method virual too) and the override keyword in the subclass.

Here is an expample:

abstract class A

{

    public abstract void M();

    public virtual void M1() { }

}

 

class C : A

{

    public override void M() { }

    public override void M1() { }

}

If don't use one of the virtual/abstract and override keyword you can still have a reimplementation of a superclass method in a subclass. This reimplementation "hides" the implementation in the base class but it doesn't overrides it. The new keyword is used to explicitly indicate the hiding. This is the default behavior when you reimplement methods in subclasses, that's why you get that warning.

This is how the new works:

class A

{

    public void M() { Console.WriteLine("SuperClass"); }

}

 

class C : A

{

    public /*new*/ void M() { Console.WriteLine("SubClass"); }

}

 

public class M

{

    static void Main(string[] args)

    {

        A c = new C();

        c.M(); //-> SuperClass

        ((C)c).M(); //-> SubClass

    }

}

On balance I think adding some override keywords as appropiate will solve your problem.