Search code examples
c#rectangles

Is there a way to add additional values to the rectangle struct?


I want to add additional values to a rectangle. Like a "Name" string for example.

Something like this:

Rectangle MyRectangle = new Rectangle(Y, X, Width, Height, Name)

Is this possible?


Solution

  • There is two overload constructor function in Rectangle class.

    public Rectangle(Point location, Size size);
    public Rectangle(int x, int y, int width, int height);
    

    But there isn't a constructor function parameter new Rectangle([int], [int], [int], [int], [string]) in Rectangle class.

    You can try to use composite public Rectangle rect { get; set; } property in the class.

    Then use constructor function to set Rectangle object and Name

    public class CustomerRectangle 
    {
        public Rectangle Rect { get; set; }
        public string Name { get; set; }
        public CustomerRectangle(int llx, int lly, int urx, int ury,string name) 
        {
            Rect = new Rectangle(llx, lly, urx, ury);
            Name = name;
        }
    }
    

    then you can use

    CustomerRectangle  MyRectangle = new CustomerRectangle (Y, X, Width, Height, Name);
    
    //MyRectangle.Name; use Name property 
    //MyRectangle.Rect; use Rectangle