473,226 Members | 1,376 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,226 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 4764
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: ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.