Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Task1
- {
- class Program
- {
- static Random rnd = new Random();
- static void Main(string[] args)
- {
- int n = 10;
- Action<object, BallEventArgs> logBallError = (s, a) => Console.WriteLine(a.Msg);
- Ball[] ballArr = Enumerable.Range(0, n)
- .Select(x =>
- {
- var b = new Ball(x);
- b.RaiseBallError += logBallError;
- return b;
- })
- .ToArray();
- for (int i = 0; i < ballArr.Length; i++)
- {
- ballArr[i].R = rnd.Next(711) - 5 + rnd.NextDouble();
- }
- Console.Read();
- //Ball ball = new Ball(10);
- //ball.RaiseBallError += (s, a) => Console.WriteLine(a.Msg);
- }
- }
- class BallEventArgs : EventArgs
- {
- public string Msg { get; set; }
- public BallEventArgs(string msg)
- {
- Msg = msg;
- }
- }
- class Ball
- {
- public static double maxR = 500;
- public event Action<object, BallEventArgs> RaiseBallError;
- double r;
- public double R
- {
- get
- {
- return R;
- }
- set
- {
- if (value > maxR || value <= 0)
- {
- Error($"Boom: Invalid radius = {value}");
- return;
- }
- }
- }
- public Ball(double r)
- {
- R = r;
- }
- private void Error(string message)
- {
- RaiseBallError?.Invoke(this, new BallEventArgs(message));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement