469,344 Members | 6,633 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,344 developers. It's quick & easy.

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 1984
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by steve | last post: by
2 posts views Thread by Dan Disney | last post: by
3 posts views Thread by N Khan via .NET 247 | last post: by
6 posts views Thread by Marcin Kalicinski | last post: by
3 posts views Thread by fh1996 | last post: by
3 posts views Thread by Aaron | last post: by
10 posts views Thread by =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post: by
12 posts views Thread by Spoon | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.