Click here to Skip to main content
15,887,837 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm building a little library for timers, just for a bit of fun and as a training in some coding practices. I came across a question and I can't find a proper answer through google (yes, I looked thoroughly, unless I'm not aware of the correct terminology). In my timer I have a property IsStarted. I want to be notified when it's value changes (each time the Start() or Stop() action is called.

On one hand I have what I call named events. This is what I use now. Let me give you an example, it will illustrate what I mean more clearly than when I try to describe it.
(I call it named events because the events are named after what happens, in this case the event fires when the IsStarted property changed.)
C#
public class MyTimer
{
  private bool _isStarted;

  public delegate EventHandler OnIsStartedChanged;

  public bool IsStarted
  {
    get { return _isStarted; }
    private set
    {
      _isStarted = value;
      // call function to indicate that the property has changed
      OnIsStartedChanged();
    }
  }
}


On the other hand I have INotifyPropertyChanged. This will save me (when I work with a load of properties I want to monitor) a lot of work in the timer class, but will give me extra work when I have to implement the callback function.

The ChangedProperty event fires and now I have to see which property changed and what I want to do with each property.

Now I'm wondering what is the best way? What is the most standard compliant? What will give me the cleanest code? Or is it just a matter of personal taste?
Posted

1 solution

C#
public interface IOnStartChanged
        { 
            public void OnIsStartedChanged(); 
        }

        public class MyTimer
        {
            private List<IOnStartChanged> on_changed_listener = new List<IOnStartChanged>;();
            private bool _isStarted;

            public void addOnChangedListener(IOnStartChanged listener)
            {
                on_changed_listener.Add(listener);
            }

            public void removeOnChangedListener(IOnStartChanged listener)
            {
                on_changed_listener.Remove(listener);
            }

            public bool IsStarted
            {
                get { return _isStarted; }
                private set
                {
                    _isStarted = value;
                    foreach (IOnStartChanged listener in on_changed_listener)
                        listener.OnIsStartedChanged();
                }
            }
        }
 
Share this answer
 
v4
Comments
KenBonny 14-May-13 2:52am    
Didn't you just write your own event handler? How is that better than using the build in C# constructs? Can you explain your reasoning?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900