.Net Framework >> Calling base implementation from base class?

by eSapient » Fri, 20 Aug 2004 00:07:32 GMT

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

Similar Threads

1. Calling base implementation from base class?

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>


2. How to call a base class of a base class - CSharp/C#

3. How to call a base class's base class method

I've got a RichTextBoxEx inherited from RichTextBox, and I want to overload
the TextLength property to use TextBoxBase's implementation of TextLength.
But since TextBoxBase isn't my immediate base class, I don't know how to
call its TextLength property.  Any ideas?

public override int TextLength
{
    get
    {
        return base.TextLength;  //Uses RichTextBox.TextLength, which I
don't want.
        return TextBoxBase.TextLength; //Doesn't compile.
        return base.base.TextLength;    //Doesn't compile.
    }
}

Any calls I make through a cast to my this pointer should just come right
back into my implementation rather than going to a base implementation since
TextLength is virtual.  So is there a way to do this in C# without using
Reflection?


4. how to make sure this.Test() call base class method in base class constructor - CSharp/C#

5. Use reflection to call base class implementation of virtual me

Whether or not this should be done is not what I'm asking.  I'm asking for a 
solution to a problem I have.  If it can't be done, fine.  If it can, I want 
to know how.

I've figured out a way to call a base class private interface implementation 
when an interface is reimplemented in a derived class (using 
Type.InterfaceMapping) and now I want to know if I can do it on public 
virtual methods.  It must be possible, after all I can code it directly in 
the language syntax.  I just can't find a reflection API method to allow me 
to do the same thing.  If I can do it one way, why am I not allowed to do the 
same thing a different way?

I find that intellectual arguments about object oriented design purity can 
be very interesting, but often get in the way of solving the real problems 
that I have right now.

"Ming Chen" wrote:

> Jay,
>    I think the user of client should NOT have the option of applying the
> virtual method implementation of a superclass to a subclass instance. The
> reason is that it may not work, even worse, it could break the application.
> Only the author of the subclass is in the position to datamine whether those
> virtual method implementations of it's parent should be exposed to client or
> not.
> 
> Regards
> Ming Chen
> 
> "Jay" < XXXX@XXXXX.COM > wrote in message
> news: XXXX@XXXXX.COM ...
> > I'm trying to use reflection to lookup and invoke a specific instance of a
> > virtual method on a class.  I can get the method easy enough, but the
> Invoke
> > method of MethodInfo states that it calls the correct virtual method based
> on
> > the passed in instance type.  The problem is it doesn't point out a way to
> > invoke a specific instance of an implementation in the inheritance
> hierarchy.
> >
> > I'm essentially attempting to invoke the base.MyMethod through reflection,
> > but can't find any way to do so.  Any ideas?
> 
> 
> 

6. Use reflection to call base class implementation of virtual method - .Net Framework

7. How to use the base keyword to call a base class method (not a constructor)

Hello,

I have read that the base keyword can be used not only to control a base
class instantiation (thus calling one of the base class constructors) but
also to access any other public or protected method in the parent class ...
I have search all over for an example of how to do this but have failed.
What I was wondering is about the syntax necessary to make such a call .....
Thx..

Bob Rock


8. Getting a reference to the base class of a base class from an inherited class - CSharp/C#