Alisson Santos
Alisson Santos

Reputation: 125

Call method from child in parent (C#)

I think my doubt is simple to solve, but I didn't found any answers for that on google, so, may you help me:

Having 2 classes, like:

class A
{
    string _Prop1;
    string _Prop2;

    public string Prop1 
    {
        get { return _Prop1; }
        set { _Prop1 = value; }
    }

    public string Prop2 
    {
        get { return _Prop2; }
        set { _Prop2 = value; }
    }

    public A() 
    {
      // CALL METHOD FROM CHILD B, PASSING PARAMETER, LIKE:
      // B objB = new B();
      // b.LoadData()
      // So, at this point, A properties would receive the sent values from B          
      // (what i really want)
    }
}

class B : A
{
    public void LoadData() 
    {
       Prop1 = "Foo";
       Prop2 = "Bar";
    }
}

And

 A objA = new A();

 Console.Write("PROP1: {0} / PROP1: {1}", objA.Prop1, objA.Prop2);

 objA.Prop1 = "Hello";
 objA.Prop2 = "World";

 Console.Write("PROP1: {0} / PROP1: {1}", objA.Prop1, objA.Prop2);

 Console.ReadKey();

So, in the main program would display: first "PROP1: Foo / PROP2: Bar" and then "PROP1: Hello / PROP2: World", BUT, the first Console.Write(), displays empty (PROP1: / PROP2:)...

Hope you help me. Thanks so much.

Upvotes: 0

Views: 266

Answers (4)

Moslem Shahsavan
Moslem Shahsavan

Reputation: 1337

this is not problem because two class has different reference you may use base class or use below to solve your problem.

A objA = new B();

((B)objA).LoadData();

Console.Write("PROP1: {0} / PROP1: {1}", objA.Prop1, objA.Prop2);

Upvotes: 0

Sayse
Sayse

Reputation: 43330

Your comment in your A constructor is wrong, you can't just create a new instance of an object and expect the calling object to retrieve those values.

You can of course assign the values to a from b after doing the load data but this is still built on the premise of creating an object for the sake of initializing one which is a bit wrong.

public A() 
{
    B b = new B();
    b.LoadData();
    this.Prop1 = b.Prop1;
    this.Prop2 = b.Prop2;
}

Instead it probably makes a lot more sense to have the load data in the base class A since it doesn't do anything special for instances of B (from what you've shown us)

Upvotes: 0

LInsoDeTeh
LInsoDeTeh

Reputation: 1038

You can solve this by making Class A abstract an define an abstract method which Class B must override. E.g.:

abstract class A {
   public string Prop1 { get; set; }
   public string Prop2 { get; set; }

   protected abstract void LoadData();

   public A() {
      //some code
      LoadData();
      //some code
   }
}

and then

class B : A {
    protected override void LoadData() {
        //Class B implementation of LoadData which can 
        //access Class A properties and init them, e.g.
        Prop1 = "foo";
        Prop2 = "bar";
    }
}

Upvotes: 1

DeshDeep Singh
DeshDeep Singh

Reputation: 1863

Use method hiding along with new and virtual keyword in class Mapper and class B as follows:

class Program
{
    static void Main(string[] args)
    {
        Mapper a = new B(); //notice this line
        B b = new B();

        a.doStuff();
        b.doStuff();

        Console.ReadLine();
    }
}

class A
{
    public void doStuff()
    {
        Console.WriteLine("A did stuff");
    }
}

class Mapper : A
{
    new public virtual void doStuff() //notice the new and virtual keywords here which will all to hide or override the base class implementation
    {
        Console.WriteLine("Mapper did stuff");
    }
}

class B : Mapper
{
    public override void doStuff()
    {
        Console.WriteLine("B did stuff");
    }
}

Upvotes: 0

Related Questions