Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 862701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:11:08+00:00 2026-05-15T09:11:08+00:00

I find that I write a lot of code within my classes to keep

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:11:09+00:00Added an answer on May 15, 2026 at 9:11 am

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an algorithm that will find the set of all vertices
Where can I find a good reference that explains how to write and install
I find that most books concerning C++ templates don't tell anything about whether it's
I find that in my daily Flex/Flash work, I do this number a lot:
Is there a c# library that can help to write and indent Javascript code.
I have several small open-source projects that I wrote. All my attempts to find
I find that getting Unicode support in my cross-platform apps a real pain in
I find that the .NET event model is such that I'll often be raising
I find that quite often Visual Studio memory usage will average ~150-300 MB of
I find that I frequently end up writing a function that I always call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.