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
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