Advertisement
Guest User

asdasda

a guest
Jul 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. namespace Dotnet
  2. {
  3. public class Dog
  4. {
  5. private string _name;
  6.  
  7. public string Name
  8. {
  9. get { return _name; }
  10. set { _name = value; }
  11. }
  12.  
  13.  
  14. public event EventHandler HasSpoken;
  15. public void Speak(string what = "bark")
  16. {
  17. if(HasSpoken != null) // se tiene que checkear si hay algun metodo que "escuche" a evento, sino tira error
  18. HasSpoken(this, EventArgs.Empty); // en caso de que halla alguien suscripto , si lo levanto
  19.  
  20. }
  21.  
  22. }
  23.  
  24. }
  25.  
  26. namespace Dotnet
  27. {
  28. public class Program
  29. {
  30. public static void Main(string[] args)
  31. {
  32. Trainer t1 = new Trainer();
  33. Dog d1 = new Dog();
  34.  
  35. d1.Speak();
  36. }
  37. }
  38.  
  39. }
  40.  
  41. namespace Dotnet
  42. {
  43. public class Trainer
  44. {
  45. void Operate()
  46. {
  47. var dog = new Dog();
  48.  
  49. dog.HasSpoken += dog_HasSpoken; // suscripcion al event
  50. }
  51.  
  52. void dog_HasSpoken(object sender, EventArgs args)
  53. {
  54. //cuando el dog llama a hasspoken se ejecuta este evento y cualquier otro que este suscrito al evento de la manra +=
  55. Console.WriteLine("Shut up");
  56. }
  57. }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement