Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ public string Name
get { return name; }
set
{
var args = new ProductEventArgs(name, value, price, price);
name = value;
/*
* TODO #4 Инициировать уведомление об
* изменении наименования
*/
NameChanged?.Invoke(this, args);
}
}
/// <summary>
Expand All @@ -47,21 +45,18 @@ public decimal Price
get { return price; }
set
{
var args = new ProductEventArgs(name, name, price, value);
price = value;
/*
* TODO #5 Инициировать уведомление об
* изменении стоимости
*/
PriceChanged?.Invoke(this, args);
}
}

#endregion

#region Events

/*
* TODO #3 Добавить определение событий
*/
public event EventHandler<ProductEventArgs> NameChanged;
public event EventHandler<ProductEventArgs> PriceChanged;

#endregion

Expand Down
29 changes: 20 additions & 9 deletions ProductEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
namespace Events
using System;

namespace Events
{
/// <summary>
/// Класс, который служит для передачи аргументов
/// в обработчик событий, возникающих в классе
/// <seealso cref="Product">Product</seealso>
/// </summary>
/*
* TODO #1 Закончить определение класса ProductEventArgs
*/
class ProductEventArgs
class ProductEventArgs : EventArgs
{
/*
* TODO #2 Добавить определение необходимых компонент
* класса ProductEventArgs
*/
public string Old_Name { get; }
public decimal Old_Price { get; }
public string New_Name { get; }
public decimal New_Price { get; }

public ProductEventArgs(string old_Name, string new_Name, decimal old_Price, decimal new_Price)
{
/*
* определение необходимых компонент
* класса ProductEventArgs
*/
Old_Name = old_Name;
New_Name = new_Name;
Old_Price = old_Price;
New_Price = new_Price;
}
}
}
29 changes: 16 additions & 13 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Events
using System;

namespace Events
{
class Program
{
Expand All @@ -18,20 +20,21 @@ internal Product Product
static void Main(string[] args)
{
Product product = new Product("Some product name", 0);

/*
* TODO #6 Назначить обработчики событий в текущем контексте
*/
product.NameChanged += OnNameChanged;
product.PriceChanged += OnPriceChanged;

/*
* TODO #7 Выполнить с экземпляром класса Product действия,
* приводящие к возникновению описанных Вами событий
*/
product.Name = "Apple";
product.Price= 10;
Console.ReadKey();
}

/*
* TODO #8 Добавить определение обработчиков событий
*/

private static void OnNameChanged(object sender, ProductEventArgs args)
{
Console.WriteLine("Name changed from {0} to {1}", args.Old_Name, args.New_Name);
}
private static void OnPriceChanged(object sender, ProductEventArgs args)
{
Console.WriteLine("Price changed from {0} to {1}", args.Old_Price, args.New_Price);
}
}
}