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
21 changes: 18 additions & 3 deletions Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Product
/// </summary>
private decimal price;

#endregion
#endregion

#region Properties

Expand All @@ -32,7 +32,13 @@ public string Name
get { return name; }
set
{
var args = new ProductNameChangedEventArgs(name, value);
name = value;
if (NameChanged != null)
{
NameChanged(this, args);
}

/*
* TODO #4 Инициировать уведомление об
* изменении наименования
Expand All @@ -47,23 +53,32 @@ public decimal Price
get { return price; }
set
{
var args = new ProductPriceChangedEventArgs(price, value);
price = value;
if (PriceChanged != null)
{
PriceChanged(this, args);
}
/*
* TODO #5 Инициировать уведомление об
* изменении стоимости
*/
}
}

#endregion
#endregion

#region Events

public event EventHandler<ProductNameChangedEventArgs> NameChanged;

public event EventHandler<ProductPriceChangedEventArgs> PriceChanged;

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

#endregion
#endregion

public Product(string name, decimal price)
{
Expand Down
35 changes: 33 additions & 2 deletions ProductEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Events
using System;
namespace Events
{
/// <summary>
/// Класс, который служит для передачи аргументов
Expand All @@ -8,11 +9,41 @@
/*
* TODO #1 Закончить определение класса ProductEventArgs
*/
class ProductEventArgs
class ProductEventArgs : EventArgs
{
public string Name { get; }
public decimal Price { get; }

public ProductEventArgs(string name, decimal price)
{
Name = name;
Price = price;
}
/*
* TODO #2 Добавить определение необходимых компонент
* класса ProductEventArgs
*/
}

class ProductNameChangedEventArgs : EventArgs
{
public string NewName { get; }
public string OldName { get; }
public ProductNameChangedEventArgs(string oldName, string newName)
{
OldName = oldName;
NewName = newName;
}
}

class ProductPriceChangedEventArgs : EventArgs
{
public decimal NewPrice { get; }
public decimal OldPrice { get; }
public ProductPriceChangedEventArgs(decimal oldPrice, decimal newPrice)
{
OldPrice = oldPrice;
NewPrice = newPrice;
}
}
}
19 changes: 17 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Events
using System;
namespace Events
{
class Program
{
Expand All @@ -18,7 +19,10 @@ internal Product Product
static void Main(string[] args)
{
Product product = new Product("Some product name", 0);

product.NameChanged += OnProductNameChanged;
product.PriceChanged += OnProductPriceChanged;
product.Name = "New IPhone";
product.Price = 10000999;
/*
* TODO #6 Назначить обработчики событий в текущем контексте
*/
Expand All @@ -29,6 +33,17 @@ static void Main(string[] args)
*/
}


private static void OnProductNameChanged(object sender, ProductNameChangedEventArgs e)
{
Console.WriteLine("Было -" + e.OldName + ". Стало -" + e.NewName);
}

private static void OnProductPriceChanged(object sender, ProductPriceChangedEventArgs e)
{

Console.WriteLine("Was - " + e.OldPrice + ". And now - " + e.NewPrice);
}
/*
* TODO #8 Добавить определение обработчиков событий
*/
Expand Down