473,324 Members | 2,531 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,324 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 2121
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.