N
N
ndbn2016-06-03 10:44:39
OOP
ndbn, 2016-06-03 10:44:39

How to correctly implement a descendant method to call an ancestor method?

Hello, I have the following set of classes:

class _Foo
{
   public string FooField; 
}

class Foo //_Foo
{
    public string FooField;
    
    public _Foo ToType()
    {
        var r = new _Foo();
        r.FooField = this.FooField;
        return r;
    }
}

class _Bar : _Foo
{
    public string BarField; 
}

class Bar : Foo //_Bar
{
    public string BarField;
    
    public _Bar ToType()
    {
        var r = new _Bar();
        r.BarField = this.BarField; 
        /// ??????       
    }
}

The Foo class is a "map" for the _Foo class.
The Bar class is a "map" for the _Bar class.
The Foo class contains a ToType() method for "ghosting" the _Foo class;
But the Bar class is an inheritor of the Foo class, and it must also be correctly cast to the _Bar class.
Please tell me how to implement the ToType() method correctly in the Bar class.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kronic, 2016-06-03
@ndbn

public _Bar ToType()
{
  return new _Bar
  {
    FooField = FooField,
    BarField = BarField
  };
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question