.NET Point.IsEmpty vs IsDefined

In my user interface class, the developer has the ability to define the location property (type System.Drawing.Point). By default, this property is initialized by Point.Empty. The internal code encapsulated by the class uses the .IsEmpty of the Point property to determine if a location has been set. If the property is not empty, the value x / y will be used. If empty, the code will try to put it using the row / column algorithm.

My question is:
I use the .IsEmpty property to determine if it has been set. To my surprise, if the developer sets the property to 0.0, it appears as Empty. Point 0,0 is valid in the chart. I also understand why .IsEmpty returns true for a value of 0.0.

1) Without creating your own class or inheriting from System.Drawing.Point, is there a way to find out if a property has been set?

The only idea I can think of is to assign a value with the value "new Point (-1, -1)" by default and test against it. Is there a better way? If not, please confirm.

I am using C # in Visual Studio 2005 and Visual Studio 2008

Thanks!

+3
source share
3 answers

There are several ways:

  • Make the property a point with a null value, so it will be "null" if you did not set it
  • Track if the setter method called anything by setting the private boolean field to true

t. either:

public Point? Location { ... }

or

public Point Location
{
    get ...
    set
    {
        _LocationSet = true;
        _Location = value;
    }
}
+5
source

Point? ( Nullable<Point>) Point. , "" null. , - Point , , , Point?, "unset", null .

.

+2

nullable Point: System.Drawing.Point? Nullable , , - .

+1

Source: https://habr.com/ru/post/1723511/


All Articles