Inheritance - Accessing two levels above mine

B

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);
}
}
}
 
F

Family Tree Mike

Bill said:
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()
 
F

Family Tree Mike

Family Tree Mike said:
winthin class C_A000.func() you can do this:

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


That was dumb... Just do this:

((C_A0) this).func();
 
P

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top