From 59dd024461c729bc525ffe29ac94f15f281336f5 Mon Sep 17 00:00:00 2001 From: rusakov-vi Date: Mon, 4 Mar 2019 22:56:30 +0300 Subject: [PATCH] Test --- Product.cs | 47 +++++++++++++--------------------- ProductEventArgs.cs | 29 +++++++++++---------- Program.cs | 61 ++++++++++++++++++++++++--------------------- 3 files changed, 64 insertions(+), 73 deletions(-) diff --git a/Product.cs b/Product.cs index f68c8c9..869feb8 100644 --- a/Product.cs +++ b/Product.cs @@ -1,57 +1,45 @@ -using System; +using System; +using System.Security.Policy; namespace Events { - /// - /// Класс должен описывать представление о товаре. - /// В рамках лабораторной работы должен являться - /// источником события - /// class Product { - #region Variables - /// - /// Наименование - /// + private string name; - /// - /// Стоимость - /// private decimal price; #endregion #region Properties - /// - /// Наименование - /// public string Name { get { return name; } set { + var args = new ProductEventArgs(name,value); name = value; - /* - * TODO #4 Инициировать уведомление об - * изменении наименования - */ + if (NameChanged != null) + { + NameChanged(this, args); + } } + } - /// - /// Стоимость - /// + public decimal Price { get { return price; } set { + var args = new ProductEventArgs(price,value); price = value; - /* - * TODO #5 Инициировать уведомление об - * изменении стоимости - */ + if (PriceChanged != null) + { + PriceChanged(this, args); + } } } @@ -59,9 +47,8 @@ public decimal Price #region Events - /* - * TODO #3 Добавить определение событий - */ + public event EventHandler> NameChanged; + public event EventHandler> PriceChanged; #endregion diff --git a/ProductEventArgs.cs b/ProductEventArgs.cs index a79a6d6..6c6e0c7 100644 --- a/ProductEventArgs.cs +++ b/ProductEventArgs.cs @@ -1,18 +1,17 @@ -namespace Events +using System; + +namespace Events { - /// - /// Класс, который служит для передачи аргументов - /// в обработчик событий, возникающих в классе - /// Product - /// - /* - * TODO #1 Закончить определение класса ProductEventArgs - */ - class ProductEventArgs + public class ProductEventArgs : EventArgs { - /* - * TODO #2 Добавить определение необходимых компонент - * класса ProductEventArgs - */ + public T OldValue { get;} + public T NewValue { get;} + + public ProductEventArgs( T oldvalue, T newvalue) + { + OldValue = oldvalue; + NewValue = newvalue; + } + } -} +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index b9247f7..7d850b0 100644 --- a/Program.cs +++ b/Program.cs @@ -1,37 +1,42 @@ -namespace Events +using System; + +namespace Events { - class Program + class Program + { + internal Product Product { - internal Product Product - { - get - { - throw new System.NotImplementedException(); - } + get + { + throw new System.NotImplementedException(); + } - set - { - throw new System.NotImplementedException(); - } - } + set + { + throw new System.NotImplementedException(); + } + } - static void Main(string[] args) - { - Product product = new Product("Some product name", 0); - - /* - * TODO #6 Назначить обработчики событий в текущем контексте - */ + static void Main(string[] args) + { + Product product = new Product("Some product name", 0); + product.NameChanged += OnNameChange; + product.PriceChanged += OnPriceChange; + /*TODO К событию прибавляем делегат*/ + product.Price = 11; + product.Name = "Some another name"; - /* - * TODO #7 Выполнить с экземпляром класса Product действия, - * приводящие к возникновению описанных Вами событий - */ - } + } - /* - * TODO #8 Добавить определение обработчиков событий - */ + private static void OnPriceChange(object sender, ProductEventArgs e) + { + //Product product = sender as Product; + Console.WriteLine("Товар '{0}' сменил цену с {1} на {2}",((Events.Product) sender).Name , e.OldValue, e.NewValue); + } + private static void OnNameChange(object sender, ProductEventArgs e) + { + Console.WriteLine("Товар '{0}' сменил имя на '{1}'", e.OldValue, e.NewValue); } + } }