Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to raise events defined using Event Properties
- 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);
- }
- }
- }
- 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);
- }
- }
- 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);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment