1

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?

Jubo
  • 41
  • 3
  • 3
    Just use inheritance, create a class MyRectangle: Rectangle and add the properties, try it, if you have problems, then ask – rekiem87 Aug 28 '18 at 21:32
  • Have you tried modifying the existing struct like you propose, or tried extending the `Rectangle` struct with your own struct? What does the compiler tell you? – Lance U. Matthews Aug 28 '18 at 21:36
  • 1
    Possible duplicate of [Adding custom property to the rectangle class](https://stackoverflow.com/questions/7294971/adding-custom-property-to-the-rectangle-class) – Lance U. Matthews Aug 28 '18 at 22:37

3 Answers3

1

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
D-Shih
  • 44,943
  • 6
  • 31
  • 51
  • 1
    I think you can't inherit from a structure.See: https://stackoverflow.com/questions/1222935/why-dont-structs-support-inheritance – Bart Aug 28 '18 at 21:54
  • 2
    `System.Drawing.Rectangle` is a **structure**, not a **class**. You can use composition and define a new struct (or class, as desired) with the additional members you want, but it won't be polymorphic with `System.Drawing.Rectangle`, unfortunately. – Jake H Aug 28 '18 at 21:57
  • Yes you are right I lost it. I edit my answer thanks. – D-Shih Aug 28 '18 at 22:00
  • Thanks this helped a lot! – Jubo Aug 28 '18 at 22:22
1

I assume you are using a constructor from the System.Drawing namespace: https://learn.microsoft.com/en-us/dotnet/api/system.drawing.rectangle?view=netframework-4.7.2

It is not possible to add an extra field to that structure. What you can do is create your own class or structure which does contain more .

public class NamedRectangle
{
    public string Name { get; set; }

    public double X { get; set; }

    public double Y { get; set; }

    public double Width { get; set; }

    public double Height { get; set; }

    public NamedRectangle(double x, double y, double width, double height, string name)
    {
        Name = name;
        X = x;
        Y = y;
        Width = width;
        Height = height;
    }
}
Bart
  • 547
  • 2
  • 12
  • 1
    You could make that a struct - it doesn't need to be a class. Or, as someone else mentioned, you could simply create a struct or class that composes a Rectangle and a string and mimic the existing Rectangular signature and behavior (but, it wouldn't be a Rectangle) – Flydog57 Aug 28 '18 at 22:19
0

I've seen others have contributed good examples, but in case of Cannot inherit from sealed type error, following example might help you:

public class myRectangle
{

    private Rectangle newRectangle = new Rectangle();
    private string name;

    public myRectangle Rectangle(Int32 Y, Int32 X, Int32 Height, Int32 Width, string name )
    {
       newRectangle.Y = Y;
       newRectangle.X = X;
       newRectangle.Height = Height;
       newRectangle.Width = Width;
       this.name = name;

       return this;
    }
}
isydmr
  • 649
  • 7
  • 16