new modifier problem

A

Aaron

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
 
J

Jon Skeet [C# MVP]

Aaron said:
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.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top