Advertisement
AskTomorrow

Untitled

Feb 8th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Task1
  8. {
  9. class Program
  10. {
  11. static Random rnd = new Random();
  12. static void Main(string[] args)
  13. {
  14. int n = 10;
  15. Action<object, BallEventArgs> logBallError = (s, a) => Console.WriteLine(a.Msg);
  16. Ball[] ballArr = Enumerable.Range(0, n)
  17. .Select(x =>
  18. {
  19. var b = new Ball(x);
  20. b.RaiseBallError += logBallError;
  21. return b;
  22. })
  23. .ToArray();
  24.  
  25. for (int i = 0; i < ballArr.Length; i++)
  26. {
  27. ballArr[i].R = rnd.Next(711) - 5 + rnd.NextDouble();
  28. }
  29.  
  30. Console.Read();
  31.  
  32. //Ball ball = new Ball(10);
  33. //ball.RaiseBallError += (s, a) => Console.WriteLine(a.Msg);
  34. }
  35. }
  36.  
  37. class BallEventArgs : EventArgs
  38. {
  39. public string Msg { get; set; }
  40.  
  41. public BallEventArgs(string msg)
  42. {
  43. Msg = msg;
  44. }
  45. }
  46.  
  47. class Ball
  48. {
  49. public static double maxR = 500;
  50.  
  51. public event Action<object, BallEventArgs> RaiseBallError;
  52.  
  53. double r;
  54. public double R
  55. {
  56. get
  57. {
  58. return R;
  59. }
  60.  
  61. set
  62. {
  63. if (value > maxR || value <= 0)
  64. {
  65. Error($"Boom: Invalid radius = {value}");
  66. return;
  67. }
  68. }
  69. }
  70.  
  71. public Ball(double r)
  72. {
  73. R = r;
  74. }
  75.  
  76. private void Error(string message)
  77. {
  78. RaiseBallError?.Invoke(this, new BallEventArgs(message));
  79. }
  80.  
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement