What your intention is seems to be a little clearer.
However, it also suggests a deeper design problem. If
details about your actual application situation were
known, an alternative workable design could be suggested.
In the absence of that, the following might help:
using System;
namespace ConsoleApplication4
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Derived d=new Derived();
d.Method1();
}
}
class Base
{
public virtual void Method1() // Does
this need to be public?
{
Console.WriteLine("Inside
Base.Method1()");
Method3();
}
protected void Method2()
{
Console.WriteLine("Inside
Base.Method2()");
Method3();
}
private void Method3()
{
Console.WriteLine("Inside
Base.Method3()");
}
}
class Derived : Base
{
public override void Method1()
{
Console.WriteLine("Inside
Derived.Method1()");
base.Method2();
}
}
}
The essence of what is different in this code is that
whatever code in Base that must be executed when
Derived.Method1() is called is moved to Base.Method3().
>-----Original Message-----
>Sorry, I pulled that sample out of thin air without
testing it. Here's
>working code to illustrate the problem:
>
>using System;
>
>namespace ConsoleApplication4
>{
> class Class1
> {
> [STAThread]
> static void Main(string[] args)
> {
> Derived d=new Derived();
> d.Method1();
> }
> }
>
> class Base
> {
> public virtual void Method1()
> {
> Console.WriteLine("Base.Method1");
> }
>
> public virtual void Method2()
> {
> Console.WriteLine("Base.Method2");
> Method1();
> }
> }
>
> class Derived : Base
> {
> public override void Method1()
> {
> Console.WriteLine("Derived.Method1");
> base.Method2();
> }
> }
>}
>
>This results in an endless loop of :
>
>Derived.Method1
>Base.Method2
>Derived.Method1
>Base.Method2
>
>because the call to Method1() in Base calls Derived's
implementation. What I
>want is for the Method1 invocation in Base to call its
(Base's)
>implementation of Method1. The desired output would be:
>
>Derived.Method1
>Base.Method2
>Base.Method1
>
>Sorry I wasn't clearer before.
>
>"eSapient" < XXXX@XXXXX.COM > wrote in message
>news:% XXXX@XXXXX.COM ...
>> Depends much on what interface you want to expose. Did
you try running
>your
>> code? It would not run because B.Method1 is protected.
So the first thing
>> you need to do is make it public. But in that case,
you would be changing
>> the signature of A.Method1 which you are overriding
and that is not
>allowed.
>> The following will work, but may not exactly meet your
requirements.
>>
>> class MyApp
>>
>> {
>>
>> public static void Main()
>>
>> {
>>
>> D d = new D();
>>
>> d.Method2();
>>
>> }
>>
>> }
>>
>>
>>
>> class B
>>
>> {
>>
>> protected virtual void Method1()
>>
>> { Console.WriteLine("Inside B.Method1()"); }
>>
>> }
>>
>> class D : B
>>
>> {
>>
>> public void Method2()
>>
>> { base.Method1(); }
>>
>> }
>>
>>
>> "Joel" < XXXX@XXXXX.COM > wrote in message
>> news: XXXX@XXXXX.COM ...
>> > Is there a way to call a base class implementation
of a protected method
>> > when Here's the scenario:
>> >
>> > class MyApp
>> > {
>> > public static Main()
>> > {
>> > D obj=new D();
>> >
>> > obj.Method1();
>> > }
>> > }
>> >
>> >
>> > class B
>> > {
>> > protected void Method1()
>> > {
>> > }
>> >
>> > protected void Method2()
>> > {
>> > //I want to call B's implementation of
Method1 even though I'm
>> > running as D's implementation of Method2;
>> > //conceptually I want to do:
this.base.Method1();
>> > }
>> > }
>> >
>> > class D : B
>> > {
>> > override protected void Method1()
>> > {
>> > Method2();
>> > }
>> > }
>> >
>> >
>> >
>> > Is this doable?
>> >
>> > TIA
>> >
>> > </joel>
>> >
>> >
>>
>>
>
>
>.
>