Hi,

I understand how there are events for things such as buttons and other form objects, but I was curious as to if it is possible to create an event for when a variables value is changed.

Thus if I have an integer variable named MyInt with value 0, it will fire the ChangedValue() method when MyInts value is increased or decreased.

Thank you in advance!

Hmm I think I may have found an answer to my own question.

I believe Accessors work the same way as events but for variables if I'm not mistaken.

Example:

private int myint; // Variable to hold my integer
public int myInt {
      get {
                return myint;
             }
      set {
               myint = value;
             }
}

This would change or get the value of the (myint) variable and I could put in code to respond when the integer is being get or set...

If anyone has any other suggestions that'd be great!

Thank you

Here's a convoluted example of creating an event that monitors a property in a class and then subscribing to it.

class Program
    {
        static void Main(string[] args)
        {
            ValueHolder valueHolder = new ValueHolder();
            valueHolder.ValueChangedEvent += new ValueHolder.ValueChangedEventHandler(valueHolder_ValueChangedEvent);
            valueHolder.Value = 7;

            Console.Read();
        }

        static void valueHolder_ValueChangedEvent(object sender, ValueChangedEventArgs e)
        {
            Console.WriteLine("old - {0}\tnew - {1}", e.OldValue, e.NewValue);
        }
    }

    public class ValueHolder
    {
        private int _value;
        public int Value
        {
            get { return _value; }
            set
            {
                if (_value != value)
                {
                    int temp = _value;
                    _value = value;
                    this.RaiseValueChangedEvent(temp, value);
                }
            }
        }

        public delegate void ValueChangedEventHandler(object sender, ValueChangedEventArgs e);
        public event ValueChangedEventHandler ValueChangedEvent;

        private void RaiseValueChangedEvent(int oldValue, int newValue)
        {
            if (ValueChangedEvent != null)
                ValueChangedEvent(this, new ValueChangedEventArgs(oldValue, newValue));
        }

    }

    public class ValueChangedEventArgs : EventArgs
    {
        public ValueChangedEventArgs(int oldValue, int newValue)
        {
            this.OldValue = oldValue;
            this.NewValue = newValue;
        }

        public int OldValue { get; set; }
        public int NewValue { get; set; }
    }

Thank you, that helped quite a bit :)

There are standard .NET interfaces for this:

public class SomeVariableClass : INotifyPropertyChanging, INotifyPropertyChanged
  {
    private int someVal;
    public int SomeVal
    {
      get { return someVal; }
      set
      {
        if (someVal != value)
        {
          FirePropertyChanging("SomeVal");
          this.someVal = value;
          FirePropertyChanged("SomeVal");
        }
      }
    }
    public SomeVariableClass()
    {
    }
    
    #region INotifyPropertyChanging Members
    public event PropertyChangingEventHandler PropertyChanging;
    #endregion

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

    private void FirePropertyChanging(string name)
    {
      var del = this.PropertyChanging;
      if (del != null)
        del(this, new PropertyChangingEventArgs(name));
    }
    private void FirePropertyChanged(string name)
    {
      var del = this.PropertyChanged;
      if (del != null)
        del(this, new PropertyChangedEventArgs(name));
    }
  }
commented: Nice! +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.