班级活动

发布于 2024-09-06 05:39:44 字数 830 浏览 13 评论 0原文

我发现我在类中编写了大量代码来保持属性彼此同步。我读过有关“类中的事件”的内容,但无法理解如何使它们满足我的需求。我可以在这里使用一些建议。

例如,在这个例子中,我总是希望使 myColor 与任何或所有 RedGreen蓝色属性。

Class myColors
    Private Property Red As Byte
    Private Property Green As Byte
    Private Property Blue As Byte
    Private Property myColor As Color
    Sub New()
        myColor = Color.FromArgb(0, 0, 0)
    End Sub
    Sub ChangeRed(ByVal r As Byte)
        Red = r
        myColor = Color.FromArgb(Red, Green, Blue)
    End Sub
    Sub ChangeBlue(ByVal b As Byte)
        Blue = b
        myColor = Color.FromArgb(Red, Green, Blue)
    End Sub
End Class

如果其中一项或多项发生更改,我希望更新 myColor。如上所述很简单,但是有没有一种方法可以处理自动执行此操作的事件,这样我就不必将 myColor = Color.FromArgb(Red, Green, Blue) 放入每个子例程中?

I find that I write a lot of code within my classes to keep properties in sync with each other. I've read about Events in Classes, but have not been able to wrap my head around how to make them work for what I'm looking for. I could use some advice here.

For example, in this one I always want to keep myColor up to date with any change whatsoever in any or all of the Red, Green or Blue properties.

Class myColors
    Private Property Red As Byte
    Private Property Green As Byte
    Private Property Blue As Byte
    Private Property myColor As Color
    Sub New()
        myColor = Color.FromArgb(0, 0, 0)
    End Sub
    Sub ChangeRed(ByVal r As Byte)
        Red = r
        myColor = Color.FromArgb(Red, Green, Blue)
    End Sub
    Sub ChangeBlue(ByVal b As Byte)
        Blue = b
        myColor = Color.FromArgb(Red, Green, Blue)
    End Sub
End Class

If one or more of those changes, I want myColor to be updated. Easy enough as above, but is there a way to work with events that would automatically do this so I don't have to put myColor = Color.FromArgb(Red, Green, Blue) in every sub routine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九厘米的零° 2024-09-13 05:39:44

在这种情况下,为什么不将 myColor 属性更改为:

Private ReadOnly Property myColor As Color
  Begin Get
    Return Color.FromArgb(Red, Green, Blue)
  End Get
End Property

这样它将始终保持同步。

In this scenario why wouldn't you change your myColor property to be this:

Private ReadOnly Property myColor As Color
  Begin Get
    Return Color.FromArgb(Red, Green, Blue)
  End Get
End Property

This way it will always be in sync.

乖乖 2024-09-13 05:39:44

在你的情况下,事件没有用。

事件被用作其他人的代码对您的代码进行操作的一种方式。您可以创建一个事件,以便您可以告诉世界您的代码正在执行的操作。您可以为颜色更改创建一个事件,每次以某种方式更改颜色时都可以引发该事件,但这对您没有好处,并且只会被使用您的类并想要执行某些操作的任何其他代码片段使用颜色改变。

事件适用于外部代码,不适用于内部类管理。正如我前面的人所说,您遇到的问题更多的是封装问题,您可以通过两种方式解决:

1)每次像您所做的那样发生更改时更新内部变量。但我建议创建一个名为 ColorChanged() 的内部私有函数,它将像您所做的那样重新计算颜色。我说创建一个函数,因为以后如果您在更改颜色时想要更多逻辑,则不必担心在 3 个地方更改它。

2)在需要时更新颜色(就像我之前的人一样)。创建一个在每次访问时计算颜色的属性。这是最简单的,并且在大多数情况下都可以完美工作。但是,如果您大量使用该属性,那么所有这些重新计算可能会成为性能问题(在这种情况下,计算颜色不是很密集,因此并不重要)。

在这种情况下,为了简单起见,我会选择选项 2,尽管性能缺陷很小……我的意思是性能缺陷很小……

In your case events are not useful.

Events are used as a way for other people's code to act upon your code. You would make an event so that you could tell the world about something your code is doing. You could make an event for color changed that you could raise every time the color is changed in someway, but this would not be for your benefit and would be simply used by any other piece of code that uses your class and wants to do something when the color is changed.

Events are for outside code, not for internal class management. As the person before me said, the issue you are having is more of an encapsulation issue you can solve 2 ways:

1) Update internal variable every time something is changed like you are doing. But i suggest making an internal private function called ColorChanged() that will recalculate the color like you are doing. I say make a function because later down the road if you wanted more logic when the color is changed, you wouldn't have to worry about changing it in 3 places.

2) Update when the color when it is requsted (like the person before me). Make a property that calculates the color every time it is accessed. This is the simplest and in most cases works perfect. But if you use that property A LOT then all those recaclculations could be a performance issue (in this case calculating the color is not very intensive so it doesn't matter).

In this case I would do option 2 for sake of simplicity despite the tiny performance drawback...and i mean tiny performance drawback...

土豪我们做朋友吧 2024-09-13 05:39:44

我会取消红色、绿色和蓝色的类变量,而只让颜色对象处理所有值。如果您想稍后读取值,可以调用 myColor.R、.G 或 .B 来获​​取所需的组件。 myColor 对象已经存储了 R、G 和 B,因此没有必要复制信息。

I would do away with the class variables for Red,Green, and Blue, and simply have the color object handle all the values. You can call myColor.R, or .G, or .B to get the component you want if you want to read the values later. The myColor object already stores R,G, and B, so no point in duplicating the information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文