473,396 Members | 1,891 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,396 software developers and data experts.

new modifier problem

Hi,

I am attempting to alter the behaviour of a base class using the new
modifier. I have hit a snag and am unable to hide the base class
member. The skeleton code is below, I have included an override class
derivation as well that does hide the member, why override does and
new doesn't I am not sure?

using System;

class Provider
{
public virtual string GetName()
{
return "I am the REAL Provider.";
}
}

class PretendProvider : Provider
{
new public string GetName()
{
return "I am the PRETEND Provider.";
}
}

class FakeProvider : Provider
{
public override string GetName()
{
return "I am the FAKE Provider.";
}
}

class Consumer
{
private static Provider m_theProvider = new Provider();

public static string TheProviderName = "Real";

public static string WhoIsYourProvider()
{
return m_theProvider.GetName();
}

public static void UsePretendProvider()
{
m_theProvider = new PretendProvider();
TheProviderName = "Pretend";
}

public static void UseFakeProvider()
{
m_theProvider = new FakeProvider();
TheProviderName = "Fake";
}
}

class MainClass
{
[STAThread]
public static void Main()
{
Console.WriteLine("New Modifier Test:\n");

Console.WriteLine(Consumer.WhoIsYourProvider());
Console.WriteLine("should be " + Consumer.TheProviderName + "\n");

// Attempt to change the consumer's provider reference.
Consumer.UsePretendProvider();

Console.WriteLine(Consumer.WhoIsYourProvider());
Console.WriteLine("should be " + Consumer.TheProviderName + "\n");

// Attempt to change the consumer's provider reference.
Consumer.UseFakeProvider();

Console.WriteLine(Consumer.WhoIsYourProvider());
Console.WriteLine("should be " + Consumer.TheProviderName + "\n");
}
}

The relevant snippet from the C# 1.2 standard seems to support what I
am trying to do:

<quote>
class Base
{
public void F() {}
}
class Derived: Base
{
new public void F() {}
}

The new modifier indicates that the F in Derived is "new", and that it
is indeed intended to hide the inherited member.

A declaration of a new member hides an inherited member only within
the scope of the new member.

class Base
{
public static void F() {}
}
class Derived: Base
{
new private static void F() {} // Hides Base.F in Derived only
}
class MoreDerived: Derived
{
static void G() { F(); } // Invokes Base.F
}

In the example above, the declaration of F in Derived hides the F that
was inherited from Base, but since the new F in Derived has private
access, its scope does not extend to MoreDerived. Thus, the call F()
in MoreDerived.G is valid and will invoke Base.F.
</quote>

Aaron
Nov 16 '05 #1
3 2129
Aaron <az****@yahoo.com> wrote:
I am attempting to alter the behaviour of a base class using the new
modifier. I have hit a snag and am unable to hide the base class
member. The skeleton code is below, I have included an override class
derivation as well that does hide the member, why override does and
new doesn't I am not sure?


See http://www.pobox.com/~skeet/csharp/faq/#override.new

Basically, because the calling code only knows about the instance as
"Provider", it doesn't see the hiding method.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
please learn OOP programming !
Nov 16 '05 #3
From the code examples I can understand the OO have not been learned
so why bother ask something you haven't learned

My direction was first to learn this topic. I don't have any Internet sites for direction but everyone can search for this one

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: steve | last post by:
Can someone give me an example of using the ’e’ pattern modifier in php regex: http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php Thanks in advance. Wish the manual had some...
2
by: Dan Disney | last post by:
I have read conflicting information on the access scope of VB.Net class declarations when no access modifier is specified. MSDN library documentation states "Classes that do not specify an access...
3
by: N Khan via .NET 247 | last post by:
Hi, I know the usage of access modifier ?public? in DLL assembliesbut I am a bit puzzled that if let say I have a public class inan exe assembly then what purpose access modifier ?public?...
6
by: Marcin Kalicinski | last post by:
Hi all, I have a problem with 'volatile' use in C++. The function get_clocks() below tries to use a 16 bit hardware counter to count time. The counter overflows very often. But an interrupt is...
3
by: fh1996 | last post by:
Documents say that "new" class modifier is only used with nested classes. "new" indicates that the class hides an inherited member of the same name." public class MyDerivedC : MyBaseC { new...
3
by: Aaron | last post by:
Hi, I am attempting to alter the behaviour of a base class using the new modifier. I have hit a snag and am unable to hide the base class member. The skeleton code is below, I have included an...
1
by: Mark Kamoski | last post by:
Hi-- Please help. What is the default access modifier value for a Sub or a Function when one has not explicitly been set. In MSDN, in "Access Types", it states... "If no access modifier...
10
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add...
1
by: muler | last post by:
"If an instance method declaration includes the sealed modifier, it must also include the override modifier." The C# Programming Language, § 10.5.5 Sealed Methods Why is this? Thanks,...
12
by: Spoon | last post by:
Hello, Does the following code invoke undefined behavior? #include <stdio.h> int main(void) { unsigned short int u = 42; printf("%u\n", u); return 0;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.