How do I create a common property for two classes?

  • 0
    Essence:
    There is a room. The room has windows, doors, cutouts in the wall, etc. (let's call them elements).
    Two classes have been created, respectively:
    class Room{}
    class Element{}

    The room and the element have standard parameters: Length, Width, Height.
    Accordingly, the room object and the element object must have properties with parameters, for example Dictionary:
    class Room { public  Dictionary<string,double> Params {get;set;} }
    class Element { public  Dictionary<string,double> Params {get;set;} }
    Room room = new Room();
    Element element = new Element();
    room.Params["Width"]; 
    element.Params["Width"];

    The room contains many elements, let's say the property List & lt; & gt;
    public List & lt; Element & gt; Elements {get; set;};
    How do I create a common property of standard Params for these two classes? If you apply inheritance, then the object of the Element class will have a list of Elements:
    element.Elements [0] ["Width"] ,
    what would be wrong.
    C# Sadie Shannon, Jan 23, 2019

  • 2 Answers
  • 0
    Create class:
    class Entity {
    public double X = 0;
    public double Y = 0;
    public double Z = 0;
    }

    class Element : Entity {
    public string color = "red";
    }

    class Room : Entity {
    public List<Element> objects;
    public void Add(Element obj) {
    objects.Add(obj);
    }
    }

    class Door : Element{
    public string style = "modern";
    }

    class Window : Element{
    public string material = "wood";
    }

    Room room = new Room();

    room.Add(new Door());
    room.Add(new Window());
    Elijah Sosa

  • 0
    If I understand correctly, then you can make an Element class that will contain the properties width, height, lengthy.

    Inherit the Room class from this class and add List Elements
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!