Re: Calling base implementation from base class?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Microsoft (joelycat_at_hotmail.com)
Date: 08/21/04


Date: Fri, 20 Aug 2004 23:18:26 -0400

I guess I'll have to start learning IL :-)

Thanks for the info. I hope MS adds this to C# soon. It doesn't come up
often but when you need it, you end up with a messy work-around.

Thanks again.

</joel>

"Brian Tyler" <brianmtyler@yahoo.com> wrote in message
news:OT2s8$rhEHA.2340@TK2MSFTNGP11.phx.gbl...
> It actually is possible at the IL level - using the CALL instruction
versus
> the CALLVIRT. I am not sure why there doesn't seem to be a C# syntax for
it.
> In my situation, I created a utility class in IL that I then merged in
with
> my assembly (multi-module assembly). Unfortunately, VS.NET doesn't give
you
> much help in such a build - makefiles are much easier.
>
> "Joel" <joelycat@hotmail.com> wrote in message
> news:%23pkQjKihEHA.596@TK2MSFTNGP11.phx.gbl...
> > That's the first thing I tried and it doesn't work.
> >
> > Check out
> >
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_10_5_4.asp.
> > Specifically the paragraph right after the first example. It explicity
> > states:
> >
> > Had the invocation in B been written ((A)this).PrintFields(), it would
> > recursively invoke the PrintFields method declared in B, not the one
> > declared in A, since PrintFields is virtual and the run-time type of
> > ((A)this) is B.
> >
> > I worked around my immediate problem but now I'm just academically
> curious.
> > It doesn't strike me that there's anything "illegal" about doing what I
> > want, it just seems like there is no syntax for it.
> >
> > Anyone from MS want to pitch in?
> >
> > </joel>
> >
> > "Luc E. Mistiaen" <luc.mistiaen@advalvas.be.no.spam> wrote in message
> > news:eG0FmChhEHA.1568@TK2MSFTNGP09.phx.gbl...
> > > Wouldn't a cast like
> > >
> > > ((Base)this).Method1 () ;
> > >
> > > in your Method2 solve your problem?
> > >
> > > /LM
> > >
> > > "Joel" <joelycat@hotmail.com> wrote in message
> > > news:eFXyydfhEHA.1356@TK2MSFTNGP09.phx.gbl...
> > > > 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" <eSapient@Yahoo.Com> wrote in message
> > > > news:%23F13YEZhEHA.3980@TK2MSFTNGP12.phx.gbl...
> > > > > 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" <joelycat@hotmail.com> wrote in message
> > > > > news:e8BVaDWhEHA.612@TK2MSFTNGP10.phx.gbl...
> > > > > > 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>
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Relevant Pages

  • Re: Calling base implementation from base class?
    ... I guess I'll have to start learning IL :-) ... > much help in such a build - makefiles are much easier. ... >> Specifically the paragraph right after the first example. ... it just seems like there is no syntax for it. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: VB or C# ?
    ... I think my learning experience should be adopted as the standard explanation ... > other things in C style languages. ... > you really must become familiar with the overall C style syntax and ... >> When we learn and master legacy C syntax and grammar we basically learn ...
    (microsoft.public.dotnet.general)
  • Re: Callling a function from another file...
    ... learning C from some old lecture notes. ... syntax they use is now deprecated, but since all compilers will happily ... standard version, including the "standard before the standard". ... It is certainly a good idea to be aware of K&R-style declarators. ...
    (comp.lang.c)
  • Re: History of French
    ... Genie essentially hit a wall after some ... She couldn't get syntax, let along pragmatics, after many ... >distract her from language learning, ...
    (sci.lang)
  • Re: What I Think Delphi Needs to Do to Survive
    ... reason to use Delphi? ... my BRIEF keymap is harder for me to part with than ObjectPascal Syntax. ... That's why Chrome isn't gonna get a lot of converts. ... I agree the framework is the major part of the learning. ...
    (borland.public.delphi.non-technical)