By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,995 Members | 1,266 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,995 IT Pros & Developers. It's quick & easy.

Inherited method

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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.