4

I am learning Events and Delegates & started with multiple events now. Just that the docs does not supply any information or code example to raising events defined in this manner.Below you can find a simple example

Sample Code

public class Person
    {
        private string _name;
        private string _phone;

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
            }
        }

        public string Phone
        {
            get { return _phone; }
            set
            {
                _phone = value;
            }
        }

        protected EventHandlerList EventDelegateCollection = new EventHandlerList();

        //define the event key
        static readonly object PhoneChangedEventKey = new object();
        public event EventHandler PhoneChanged
        {
            add
            {
                EventDelegateCollection.AddHandler(PhoneChangedEventKey, value);
            }
            remove
            {
                EventDelegateCollection.RemoveHandler(PhoneChangedEventKey, value);
            }
        }
    }

I would like to raise the event when the Phone number is set. if anything sounds funky and don't understand what i am talking about see here

Update

I would like to clear some doubts here. There are Two ways you can actually subscribe and invoke the event handlers the classical pattern(as described here) where the steps are

  • Define the delegate that acts as signature for subscribed methods.
  • Define the Event that delegates
  • Define the method that raises the handlers note: above method creates field for every event hence consumes more memory reference

Event Property is another way where you do below

  • Define a object that acts as Key to a event
  • Define a method to add and remove handlers for the event from the event invocation list
  • Raise the event by determining event handlers based on event key
Community
  • 1
  • 1
Deeptechtons
  • 10,945
  • 27
  • 96
  • 178
  • This MSDN link describes raising the event: http://msdn.microsoft.com/en-us/library/9aackb16%28v=VS.80%29.aspx – H H Oct 15 '11 at 06:19
  • possible duplicate of [How to dispatch events in C#](http://stackoverflow.com/questions/2448487/how-to-dispatch-events-in-c-sharp) – H H Oct 15 '11 at 06:20
  • 1
    @HenkHolterman i repeat that is classical way to invoke event handlers, Event Properties is another way look here http://msdn.microsoft.com/en-us/library/8843a9ch.aspx – Deeptechtons Oct 15 '11 at 06:23
  • That page also lists the `OnMouseDown()` and `OnMouseUp()` methods so what is your exact question then? – H H Oct 15 '11 at 06:27
  • @HenkHolterman thank you, solved the question myself and code is available below. – Deeptechtons Oct 15 '11 at 06:29
  • @Anyone who wanted to close the question, please see the update of why this question differs from other related questions – Deeptechtons Oct 15 '11 at 06:40

2 Answers2

4

This is how you should actually raise it

Code

public class Person
{
    private string _name;
    private string _phone;

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
        }
    }

    public string Phone
    {
        get { return _phone; }
        set
        {
            _phone = value;
            //Invoke the Handlers now
            OnPhoneChanged();
        }
    }

    protected EventHandlerList EventDelegateCollection = new EventHandlerList();
    static readonly object PhoneChangedEventKey = new object();
    public event EventHandler PhoneChanged
    {
        add
        {
            EventDelegateCollection.AddHandler(PhoneChangedEventKey, value);
        }
        remove
        {
            EventDelegateCollection.RemoveHandler(PhoneChangedEventKey, value);
        }
    }

    private void OnPhoneChanged()
    {
        EventHandler subscribedDelegates = (EventHandler)this.EventDelegateCollection[PhoneChangedEventKey];
        subscribedDelegates(this, EventArgs.Empty);
    }
}
Community
  • 1
  • 1
Deeptechtons
  • 10,945
  • 27
  • 96
  • 178
-1

I suggest you to read Delegates and Events in C#. The code below is what you want.

public class Person
{
    public event EventHandler<string> PhoneNumberChanged;

    private string _name;
    private string _phone;

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
        }
    }

    public string Phone
    {
        get { return _phone; }
        set
        {
            _phone = value;

            if (this.PhoneNumberChanged != null)
            {
                this.PhoneNumberChanged(this._phone);
            }
        }
    }
}
Ekk
  • 5,627
  • 19
  • 27