.NET Point.IsEmpty 与 IsDefined

编程入门 行业动态 更新时间:2024-10-23 19:22:53
本文介绍了.NET Point.IsEmpty 与 IsDefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在我的 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

谢谢!

推荐答案

有几种方法:

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

即.要么:

public Point? Location { ... }

或:

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

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

更多推荐

[db:关键词]

本文发布于:2023-05-01 12:37:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409406.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Point   NET   IsDefined   IsEmpty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!