Question

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?

Was it helpful?

Solution

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...

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top