Accessing parent-parent method.

  • Thread starter Thread starter Hadi
  • Start date Start date
H

Hadi

Hello,

Say I have three classes inheritance A <-- B <-- C

And in A I have the Create method:

class A
{
public override bool Create()
{
// do stuff
return true;
}
}

class B : A
{
public override bool Create()
{
if (!base.Create())
{
return false;
}

// do stuff.
return true;
}
}

Now in class C

class C : B
{
public override bool Create()
{
// Here I need to access A.Create not B.Create()
}
}
}

Now the problem in C I want to access A.Create() not B.Create() is this
possible? Because calling base.Create() will call B.Create().

I need to do this weird inheritance because C shares a lot of methods, and
logically extends B .. but in creation C is totally different from B.

Thanks for any help.

Hadi
 
If B.Create has to be different, then you might want to think about doing:

class A {} // Note this is non abstract and creatable
abstract class Intermediary : A {}// Note this is an abstract class, put shared
methods here.
class B {} : Intermediary
class C {} : Intermediary

By calling base.Create() in B/C you now call directly down to the class A
implementation as long as you don't override the method in Intermediary. If you
do override the method in Intermediary then all you have to do is call return
base.Create() as implementation in Intermediary.

Adding full source below my sig. Note I used A,B,C,D as names where B was the
Intermediary class and C/D were you B/C classes.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

using System;

public class A {
public virtual bool Create() {
Console.WriteLine("Create From A");
return true;
}
}

public abstract class B : A {
public virtual void SharedMethod1() {
}

public virtual void SharedMethod2() {
}
}

public class C : B {
public override bool Create() {
Console.WriteLine("Create From C");
return base.Create();
}
}

public class D : B {
public override bool Create() {
Console.WriteLine("Create From D");
return base.Create();
}
}

public class Tester {
private static void Main(string[] args) {
C c = new C();
c.Create();

D d = new D();
d.Create();
}
}
 
in class C you can write base.B() thus it will call Create() method in class
B, which in its turn will call method in class A.
 
Right .. ok thanks that's is helpful.

Hadi

Justin Rogers said:
If B.Create has to be different, then you might want to think about doing:

class A {} // Note this is non abstract and creatable
abstract class Intermediary : A {}// Note this is an abstract class, put shared
methods here.
class B {} : Intermediary
class C {} : Intermediary

By calling base.Create() in B/C you now call directly down to the class A
implementation as long as you don't override the method in Intermediary. If you
do override the method in Intermediary then all you have to do is call return
base.Create() as implementation in Intermediary.

Adding full source below my sig. Note I used A,B,C,D as names where B was the
Intermediary class and C/D were you B/C classes.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

using System;

public class A {
public virtual bool Create() {
Console.WriteLine("Create From A");
return true;
}
}

public abstract class B : A {
public virtual void SharedMethod1() {
}

public virtual void SharedMethod2() {
}
}

public class C : B {
public override bool Create() {
Console.WriteLine("Create From C");
return base.Create();
}
}

public class D : B {
public override bool Create() {
Console.WriteLine("Create From D");
return base.Create();
}
}

public class Tester {
private static void Main(string[] args) {
C c = new C();
c.Create();

D d = new D();
d.Create();
}
}

Hadi said:
Hello,

Say I have three classes inheritance A <-- B <-- C

And in A I have the Create method:

class A
{
public override bool Create()
{
// do stuff
return true;
}
}

class B : A
{
public override bool Create()
{
if (!base.Create())
{
return false;
}

// do stuff.
return true;
}
}

Now in class C

class C : B
{
public override bool Create()
{
// Here I need to access A.Create not B.Create()
}
}
}

Now the problem in C I want to access A.Create() not B.Create() is this
possible? Because calling base.Create() will call B.Create().

I need to do this weird inheritance because C shares a lot of methods, and
logically extends B .. but in creation C is totally different from B.

Thanks for any help.

Hadi
 
Hadi,

you have several options to do what you need, here you go with two quick
ones:


// Solution #1: adding an intermediate access method

class A
{
public virtual bool Create()
{
return true;
}
}

class B : A
{
public override bool Create()
{
//
}

// This is the intermediate access method which can be called from
// derived classes instead of base.Create which would call B's Create
method
protected bool CreateBase()
{
return base.Create();
}
}

class C : B
{
public override bool Create()
{
// Instead of calling base.Create() you call base.CreateBase()
return base.CreateBase();
}
}

// Solution #2: Condition checking
class A
{
public virtual bool Create()
{
return true;
}
}

class B : A
{
public override bool Create()
{
// You check the type of current instance and then do different
processing
if (this is C)
{
return base.Create();
}
else
{
// here you do whatever you need to do if current instance is
not C
}
}
}

class C : B
{
public override bool Create()
{
// You simply call base.Create() and let the base class decide what
to do
return base.Create();
}
}


I am sure you could find as many solutions as you like to this problem, just
play around with it.

Regards,
 
Back
Top