班级活动
我发现我在类中编写了大量代码来保持属性彼此同步。我读过有关“类中的事件”的内容,但无法理解如何使它们满足我的需求。我可以在这里使用一些建议。
例如,在这个例子中,我总是希望使 myColor
与任何或所有 Red
、Green
或蓝色
属性。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,为什么不将 myColor 属性更改为:
这样它将始终保持同步。
In this scenario why wouldn't you change your myColor property to be this:
This way it will always be in sync.
在你的情况下,事件没有用。
事件被用作其他人的代码对您的代码进行操作的一种方式。您可以创建一个事件,以便您可以告诉世界您的代码正在执行的操作。您可以为颜色更改创建一个事件,每次以某种方式更改颜色时都可以引发该事件,但这对您没有好处,并且只会被使用您的类并想要执行某些操作的任何其他代码片段使用颜色改变。
事件适用于外部代码,不适用于内部类管理。正如我前面的人所说,您遇到的问题更多的是封装问题,您可以通过两种方式解决:
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...
我会取消红色、绿色和蓝色的类变量,而只让颜色对象处理所有值。如果您想稍后读取值,可以调用 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.