Home All Groups Group Topic Archive Search About

Inheritance - Accessing two levels above mine

Author
19 Dec 2008 1:12 AM
Bill
Hi,

How can I access a (non-static) member method that is two levels above
mine, from a member method that has the same name?

How can I access C_A0.func() from inside C_A000.func() ???

Is this possible?

Thanks a lot.
Bill


// ----------------------------
namespace Project0
{
  public class C_A0
  {
    public int func()
    {
      return(1);
    }
  }

  public class C_A00 : C_A0
  {
    new public int func()
    {
      return(2);
    }
  }

  public class C_A000 : C_A00
  {
    new public int func()
    {
      // How can I access C_A0.func() from here??
      // It does not let me write "base.base.func()". I can access the
first "base", but not the second one.
      // It does not let me write "C_A0.func()", because that function
is not static.
      return(3);
    }
  }
}

Author
19 Dec 2008 1:53 AM
Family Tree Mike
Show quote Hide quote
"Bill" <a@a.a> wrote in message
news:v5tlk4hqcon1qbkttui83tumbk1rn6gsij@4ax.com...
> Hi,
>
> How can I access a (non-static) member method that is two levels above
> mine, from a member method that has the same name?
>
> How can I access C_A0.func() from inside C_A000.func() ???
>
> Is this possible?
>
> Thanks a lot.
> Bill
>
>
> // ----------------------------
> namespace Project0
> {
>  public class C_A0
>  {
>    public int func()
>    {
>      return(1);
>    }
>  }
>
>  public class C_A00 : C_A0
>  {
>    new public int func()
>    {
>      return(2);
>    }
>  }
>
>  public class C_A000 : C_A00
>  {
>    new public int func()
>    {
>      // How can I access C_A0.func() from here??
>      // It does not let me write "base.base.func()". I can access the
> first "base", but not the second one.
>      // It does not let me write "C_A0.func()", because that function
> is not static.
>      return(3);
>    }
>  }
> }
>


winthin class C_A000.func() you can do this:

( (C_A0) ((C_A00) this) ).func()

--
Mike
Are all your drivers up to date? click for free checkup

Author
19 Dec 2008 2:08 AM
Family Tree Mike
Show quote Hide quote
"Family Tree Mike" <F**@ThisOldHouse.com> wrote in message
news:EE42C9E5-5A3C-4636-8587-71B951F744D8@microsoft.com...
> "Bill" <a@a.a> wrote in message
> news:v5tlk4hqcon1qbkttui83tumbk1rn6gsij@4ax.com...
>> Hi,
>>
>> How can I access a (non-static) member method that is two levels above
>> mine, from a member method that has the same name?
>>
>> How can I access C_A0.func() from inside C_A000.func() ???
>>
>> Is this possible?
>>
>> Thanks a lot.
>> Bill
>>
>>
>> // ----------------------------
>> namespace Project0
>> {
>>  public class C_A0
>>  {
>>    public int func()
>>    {
>>      return(1);
>>    }
>>  }
>>
>>  public class C_A00 : C_A0
>>  {
>>    new public int func()
>>    {
>>      return(2);
>>    }
>>  }
>>
>>  public class C_A000 : C_A00
>>  {
>>    new public int func()
>>    {
>>      // How can I access C_A0.func() from here??
>>      // It does not let me write "base.base.func()". I can access the
>> first "base", but not the second one.
>>      // It does not let me write "C_A0.func()", because that function
>> is not static.
>>      return(3);
>>    }
>>  }
>> }
>>
>
>
> winthin class C_A000.func() you can do this:
>
> ( (C_A0) ((C_A00) this) ).func()
>
> --
> Mike
>


That was dumb...  Just do this:

((C_A0) this).func();

--
Mike
Author
19 Dec 2008 8:34 AM
Peter Morris
As Mike said you can just type cast "this" to the base class. Note though
that if the method is virtual and overridden this wont work, and trying to
execute a specific base class's implementation of a virtual method (other
than your direct ancestor class) is a sign of a bad design.




Author
19 Dec 2008 11:52 AM
Bill
Thanks to both of you. It worked.
I posted a new message with a sligthly different question, now with
"virtual+override" instead of "new".

Bookmark and Share