1

If i have 2 classes One for data, for example:

public class Cords
{
    public double x;
    public double y;
}

and one, that using this data:

public class Geometry
{
    public Cords()
    {
        points = new List<Cords>();
    }
    public void SomeMathWithPoints()
    {
         MagicWithPoints(points);
    }

    protected List<Cords> points;
}

And i want to exted this class with some specific functions, using inheritance, but this time i need some aditional data for Cords class. So i'm trying to do it this way:

public class ExtendedCords: Cords
{
    public double x;
    public double y;
    public string name;
}

public class ExtendedGeometry : Geometry
{
     protected SomeNewMagicWithPoints(){...}
     protected List<ExtendedCords> points;
}

But i've noticed, that if i will do:

    ExtendedGeometry myObject = new ExtendedGeometry();
    myObject.SomeMathWithPoints();

This function will be using old (parrents) field points. So how can i make it use the new one with a type ExtendedCords? I mean, i want to be able to use both child's and parrent's functions on a new field.

CrazyWu
  • 647
  • 2
  • 8
  • 19
  • Your code doesn't compile. Please post code that compiles. – Patrick Hofman Feb 05 '18 at 08:30
  • What warnings do you get in your Error List? There are quite a few of them. – Patrick Hofman Feb 05 '18 at 08:31
  • 2
    You should read about [the Liskov Substitution Principle](https://stackoverflow.com/questions/56860/what-is-an-example-of-the-liskov-substitution-principle) – Pac0 Feb 05 '18 at 08:33
  • @PatrickHofman it's a kind of compressed example of what am it trying to do with my old class (the original one is pretty big, so i didn't post it here). Problem, that i'm having at the moment is that during debug i see, that `myObjecy` of an extended class is still using parrents filed with parrents function, while the new one keeps empty. And i don't know the way to pass it to the parrent. – CrazyWu Feb 05 '18 at 08:40
  • Pretty much as @Pac0 stated. Have a look at the [SOLID](https://stackoverflow.com/a/784035/3766034) principles. Once and only once you understand those fully, deeply and entirely, refer to the Stackoverflow question [how to override a method](https://stackoverflow.com/a/1853908/3766034) and on [how to override a property](https://stackoverflow.com/a/42723961/3766034). – Jirajha Feb 05 '18 at 08:40

1 Answers1

6

Use generic types for the Geometry base class and virtual methods:

public class Geometry<TCord> where TCord : Cords
{
    public void InitCords(){
        points = new List<TCord>();
    }
    public virtual void SomeMathWithPoints(){
         MagicWithPoints(points);
    };

    protected List<TCord> points;
}

Then in your extension,

public class ExtendedGeometry : Geometry<ExtendedCords>
{
     public override SomeNewMagicWithPoints(){...}
     // no need for a redefinition of points since it is inherited from the base class Geometry<ExtendedCords>
}
Georg
  • 5,626
  • 1
  • 23
  • 44
  • Thanks, but one small question if i'm creating/adding new `Cords` element into the points list, how can i redefine a type from Cords to ExtendedCrods (if needed)? I mean, `points.Add((TCords)newPoint)` – CrazyWu Feb 05 '18 at 09:07