Deeptechtons
Deeptechtons

Reputation: 11125

How to raise events defined using Event Properties

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

Event Property is another way where you do below

Upvotes: 4

Views: 10733

Answers (2)

Deeptechtons
Deeptechtons

Reputation: 11125

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);
    }
}

Upvotes: 4

Ekk
Ekk

Reputation: 5715

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);
            }
        }
    }
}

Upvotes: -1

Related Questions