.NET Point.IsEmpty 与 IsDefined [英] .NET Point.IsEmpty vs IsDefined

查看:46
本文介绍了.NET Point.IsEmpty 与 IsDefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 UI 类中,开发人员可以选择定义位置属性(System.Drawing.Point 类型).默认情况下,此属性初始化为 Point.Empty.类封装的内部代码使用 Point 属性的 .IsEmpty 来确定是否已设置位置.如果该属性不为空,则将使用 x/y 值.如果为空,代码将尝试使用行/列算法放置它.

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.

我的问题:
我正在使用该属性的 .IsEmpty 来确定它是否已设置.令我惊讶的是,如果开发人员将属性设置为 0,0,它会显示为 Empty.点 0,0 在图形中有效.我也理解为什么 .IsEmpty 为 0,0 值返回 true.

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) 如果不创建自己的类或继承自 System.Drawing.Point,有没有办法知道该属性是否已设置?

1) Without creating my own class or inheriting from System.Drawing.Point, is there a way to know if the property was set?

我能想到的唯一想法是默认属性值为new Point(-1,-1)"并对此进行测试.有没有更好的办法?如果不是,请确认.

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.

我在 Visual Studio 2005 和 Visual Studio 2008 中使用 C#

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

谢谢!

推荐答案

有几种方法:

  1. 把属性设为可以为空的Point,这样的话,没设置的时候就是null"
  2. 通过将私有布尔字段设置为 true 来跟踪是否有任何东西调用了 setter 方法

即.要么:

public Point? Location { ... }

或:

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

这篇关于.NET Point.IsEmpty 与 IsDefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆