Events in Classes (VB.NET)

Posted by Otaku on Stack Overflow See other posts from Stack Overflow or by Otaku
Published on 2010-06-17T18:10:47Z Indexed on 2010/06/17 18:23 UTC
Read the original article Hit count: 260

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?

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about object-oriented-design