.NET Point.IsEmpty vs IsDefined
c#, system.drawing
Solution
There's a few ways:
- Make the property a nullable Point, this way, it will be "null" when you haven't set it
- Track whether anything has called the setter method by setting a private Boolean field to true
ie. either:
public Point? Location { ... }
or:
public Point Location
{
get ...
set
{
_LocationSet = true;
_Location = value;
}
}
Problem
In my UI class, a developer has the option to define a location property (type of System.Drawing.Point). By default this property is initialized to Point.Empty. The internal code that is 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 x/y value will be used. If empty, the code will attempt to place it with a row/column algorythm. My Issue: I am using the .IsEmpty of the property to determine if it was set. To my surprise, if a developer sets the property to 0,0 it came up as Empty. A point of 0,0 is valid in graphics. I also understand why the .IsEmpty returns true for the 0,0 value. 1) Without creating my own class or inheriting from System.Drawing.Point, is there a way to know if the property was set? The only idea that I can think of is to default the property with a value of "new Point(-1,-1)" and test against that. Is there a better way? If not, please confirm. I am using C# in Visual Studio 2005 and Visual Studio 2008 Thanks!