473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 4770
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: thechaosengine | last post by:
Hi all, Is there a way to hide a member in a subclass that has been inherited from a base class? Lets leave aside any issues regarding whether its a good idea for a moment. Here's an example...
4
by: Dan | last post by:
I have a need to make a set of classes that all share the same public methods, some implementation and some data. So, I made an abstract base (BaseClass) with an interface (IBaseClass) and a...
10
by: Chad Miller | last post by:
I currently have a base form that I inherit. The base for has a custom event. The event will not raise threw the inherited form. I was wondering if events work threw inheritance or should I use...
13
by: Lorne Smith | last post by:
Hi, First, sorry for the crosspost, but it seemed appropriate... :) I've come accross what I consider to be a bug, but I don't know if it's already known or not. (VS .Net 2003 Pro - VB.Net) ...
3
by: Paul Michaud | last post by:
Consider the following: Class A { .... } Class B:A { ....
3
by: dbuchanan | last post by:
Can inherited code call derived code? If so how. I have identical 'generic' code that I am repeating again and again in several derived form because I don't know how to get inherited code to call...
5
by: PIEBALD | last post by:
I was trying to break some polymorphism, expecting it not to work, but I'm a curious sort. I was seeing what happens when a derived class tries to hide an inherited method with a private new...
5
by: Eliseu Rodrigues | last post by:
Hi I would like to have a static method on a base class that executes some action (for example retrieves the row count) on a table whose name is the same of the inherited class name. For...
12
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a class which "BiologySequence" which looks about like this. public class BiologySequence { private string _Sequence; public string Sequence {
8
by: Fuzzyman | last post by:
Hello all, I may well be being dumb (it has happened before), but I'm struggling to fix some code breakage with Python 2.6. I have some code that looks for the '__lt__' method on a class: ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.