Guest User

Untitled

a guest
Aug 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. How to raise events defined using Event Properties
  2. public class Person
  3. {
  4. private string _name;
  5. private string _phone;
  6.  
  7. public string Name
  8. {
  9. get { return _name; }
  10. set
  11. {
  12. _name = value;
  13. }
  14. }
  15.  
  16. public string Phone
  17. {
  18. get { return _phone; }
  19. set
  20. {
  21. _phone = value;
  22. }
  23. }
  24.  
  25. protected EventHandlerList EventDelegateCollection = new EventHandlerList();
  26.  
  27. //define the event key
  28. static readonly object PhoneChangedEventKey = new object();
  29. public event EventHandler PhoneChanged
  30. {
  31. add
  32. {
  33. EventDelegateCollection.AddHandler(PhoneChangedEventKey, value);
  34. }
  35. remove
  36. {
  37. EventDelegateCollection.RemoveHandler(PhoneChangedEventKey, value);
  38. }
  39. }
  40. }
  41.  
  42. public class Person
  43. {
  44. private string _name;
  45. private string _phone;
  46.  
  47. public string Name
  48. {
  49. get { return _name; }
  50. set
  51. {
  52. _name = value;
  53. }
  54. }
  55.  
  56. public string Phone
  57. {
  58. get { return _phone; }
  59. set
  60. {
  61. _phone = value;
  62. //Invoke the Handlers now
  63. OnPhoneChanged();
  64. }
  65. }
  66.  
  67. protected EventHandlerList EventDelegateCollection = new EventHandlerList();
  68. static readonly object PhoneChangedEventKey = new object();
  69. public event EventHandler PhoneChanged
  70. {
  71. add
  72. {
  73. EventDelegateCollection.AddHandler(PhoneChangedEventKey, value);
  74. }
  75. remove
  76. {
  77. EventDelegateCollection.RemoveHandler(PhoneChangedEventKey, value);
  78. }
  79. }
  80.  
  81. private void OnPhoneChanged()
  82. {
  83. EventHandler subscribedDelegates = (EventHandler)this.EventDelegateCollection[PhoneChangedEventKey];
  84. subscribedDelegates(this, EventArgs.Empty);
  85. }
  86. }
  87.  
  88. public class Person
  89. {
  90. public event EventHandler<string> PhoneNumberChanged;
  91.  
  92. private string _name;
  93. private string _phone;
  94.  
  95. public string Name
  96. {
  97. get { return _name; }
  98. set
  99. {
  100. _name = value;
  101. }
  102. }
  103.  
  104. public string Phone
  105. {
  106. get { return _phone; }
  107. set
  108. {
  109. _phone = value;
  110.  
  111. if (this.PhoneNumberChanged != null)
  112. {
  113. this.PhoneNumberChanged(this._phone);
  114. }
  115. }
  116. }
  117. }
Add Comment
Please, Sign In to add comment