469,267 Members | 1,029 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

Inherited method

I got a method in my ancestor form declared as Protected, this method has
empty body.
In my descendant form I declared as Protected also, then compile has no
problem but the name of the method has green underline.

The warning of the compilation is about the method hides inherited member.
Use the new keyword if hiding was intened.
Nov 7 '06 #1
4 4619
Did you perhaps intend to do:

/////////////////////////////
abstract class ancestor
{
protected virutal mymethod() {}
}
/////////////////////////////
class myclass : ancestor
{
protected override mymethod() {}
}
//////////////////////////////

-Jeroen
Alan T wrote:
I got a method in my ancestor form declared as Protected, this method has
empty body.
In my descendant form I declared as Protected also, then compile has no
problem but the name of the method has green underline.

The warning of the compilation is about the method hides inherited member.
Use the new keyword if hiding was intened.
Nov 7 '06 #2
Hi Alan,
basically what the compiler is telling you that a method in your derived
class is hiding a method in the base class. It is possible you want to do
this, if so you want to add the new keyword to the method signature in the
derived class which explicitly states you know you are hiding a method and
intended to do so. So for example:

class Person
{
public string Description()
{
return "I am a person";
}
}

class Man : Person
{
public string Description()
{
return "I am a man";
}
}

class Program
{
static void Main(string[] args)
{
Man m = new Man();

//Outputs "I am a man"
Console.Out.WriteLine(m.Description());

Person p = m;

//Outputs "I am a person"
Console.Out.WriteLine(p.Description());
}
}

The method called on the variable is defined by it's runtime type, so you
can see the description method in the Man class hides the implementation in
the base Person class when accessing it with a reference of type man. If you
want to get rid of the compiler warning you can use the new keyword i.e.:

public new string Description()

If you want to actually change the implementation of the method in the
derived class then you need to make the method virtual in the base class and
then override the method in the derived class.

Mark.
--
http://www.markdawson.org
"Alan T" wrote:
I got a method in my ancestor form declared as Protected, this method has
empty body.
In my descendant form I declared as Protected also, then compile has no
problem but the name of the method has green underline.

The warning of the compilation is about the method hides inherited member.
Use the new keyword if hiding was intened.
Nov 7 '06 #3
The method called on the variable is defined by it's runtime type, so you
can see the description method in the Man class hides the implementation in
the base Person class when accessing it with a reference of type man
should be:

The method called on the variable is defined by it's runtime type, so you
can see the description method in the Man class hides the implementation in
the base Person class when accessing it with a reference of type PERSON
--
http://www.markdawson.org
"Mark R. Dawson" wrote:
Hi Alan,
basically what the compiler is telling you that a method in your derived
class is hiding a method in the base class. It is possible you want to do
this, if so you want to add the new keyword to the method signature in the
derived class which explicitly states you know you are hiding a method and
intended to do so. So for example:

class Person
{
public string Description()
{
return "I am a person";
}
}

class Man : Person
{
public string Description()
{
return "I am a man";
}
}

class Program
{
static void Main(string[] args)
{
Man m = new Man();

//Outputs "I am a man"
Console.Out.WriteLine(m.Description());

Person p = m;

//Outputs "I am a person"
Console.Out.WriteLine(p.Description());
}
}

The method called on the variable is defined by it's runtime type, so you
can see the description method in the Man class hides the implementation in
the base Person class when accessing it with a reference of type man. If you
want to get rid of the compiler warning you can use the new keyword i.e.:

public new string Description()

If you want to actually change the implementation of the method in the
derived class then you need to make the method virtual in the base class and
then override the method in the derived class.

Mark.
--
http://www.markdawson.org
"Alan T" wrote:
I got a method in my ancestor form declared as Protected, this method has
empty body.
In my descendant form I declared as Protected also, then compile has no
problem but the name of the method has green underline.

The warning of the compilation is about the method hides inherited member.
Use the new keyword if hiding was intened.

Nov 7 '06 #4

"Mark R. Dawson" <Ma*********@discussions.microsoft.comwrote in message
news:3D**********************************@microsof t.com...
>The method called on the variable is defined by it's runtime type, so you
can see the description method in the Man class hides the implementation
in
the base Person class when accessing it with a reference of type man

should be:

The method called on the variable is defined by it's runtime type, so you
can see the description method in the Man class hides the implementation
in
the base Person class when accessing it with a reference of type PERSON
--
That's only true for virtual methods, so you were right the first time.
Nov 7 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by thechaosengine | last post: by
4 posts views Thread by Dan | last post: by
10 posts views Thread by Chad Miller | last post: by
3 posts views Thread by Paul Michaud | last post: by
3 posts views Thread by dbuchanan | last post: by
5 posts views Thread by PIEBALD | last post: by
12 posts views Thread by =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.