1

I wish to run an event which will occur on some counter change, e.g. every time

int counter;

changes its value, the event is raised. I have something like from MSDN:

public class CounterChange:INotifyPropertyChanged
{
    private int counter;
    // Declare the event
    public event PropertyChangedEventHandler PropertyChanged;

    public CounterChange()
    {
    }

    public CounterChange(int value)
    {
        this.counter = value;
    }

    public int Counter
    {
        get { return counter; }
        set
        {
            counter = value;
            // Call OnPropertyChanged whenever the property is updated
            OnPropertyChanged("Counter");
        }
    }

    // Create the OnPropertyChanged method to raise the event
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if(handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

But have no idea what next. How to raise incrementation from program, and connect method to these event.

H H
  • 263,252
  • 30
  • 330
  • 514
santBart
  • 2,466
  • 9
  • 43
  • 66
  • 1
    possible duplicate of [How to dispatch events in C#](http://stackoverflow.com/questions/2448487/how-to-dispatch-events-in-c-sharp) – H H Feb 21 '12 at 09:37
  • Some information that isn't included in your question is an explanation of what code should be handling this event. For example, are you writing an event handler yourself, or are you binding the property to some UI? The code sample you show above is specific to WPF/SL (and other generic frameworks). – Drew Noakes Feb 21 '12 at 09:39

2 Answers2

3

You'd probably have to do something like this in your main program:

var counter = new CounterChange(0);
counter.PropertyChanged += SomeMethodYouWantToAssociate;

So, when the value of counter.Counter changes, the subscribers of the event will be notified and executed (in my example, SomeMethodYouWantToAssociate will be).

private static void SomeMethodYouWantToAssociate(object sender, PropertyChangedEventArgs e)
{
    // Some Magic inside here
}
Mattia Vitturi
  • 235
  • 4
  • 17
0
public class CounterClass
{
    private int counter;
    // Declare the event
    public event EventHandler CounterValueChanged;

    public CounterChange()
    {
    }

    public CounterChange(int value)
    {
        this.counter = value;
    }

    public int Counter
    {
        get { return counter; }
        set
        {
            //Chaeck if has really changed?
            if(counter != value)
            {
                counter = value;
                // Call CounterValueChanged whenever the property is updated
                //check if there are any subscriber to this event
                if(CounterValueChanged!=null)
                    CounterValueChanged(this, new EventArgs());
            }
        }
    }
}

And use this class like this

CounterClass cnt = new CounterClass();
cnt.CounterValueChanged += MethodDelegateHere;
Maheep
  • 5,539
  • 3
  • 28
  • 47