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
13 changes: 12 additions & 1 deletion Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ public string Name
get { return name; }
set
{
var args = new ProductEventArgs(name, value);
name = value;
/*
* TODO #4 Инициировать уведомление об
* изменении наименования
*/
if (NameChanged != null)
{
NameChanged(this, args);
}
}
}
/// <summary>
Expand All @@ -47,11 +52,16 @@ public decimal Price
get { return price; }
set
{
var args = new ProductEventArgs(price.ToString(), value.ToString());
price = value;
/*
* TODO #5 Инициировать уведомление об
* изменении стоимости
*/
if (PriceChanged != null)
{
PriceChanged(this, args);
}
}
}

Expand All @@ -62,7 +72,8 @@ public decimal Price
/*
* TODO #3 Добавить определение событий
*/

public event EventHandler<ProductEventArgs> NameChanged;
public event EventHandler<ProductEventArgs> PriceChanged;
#endregion

public Product(string name, decimal price)
Expand Down
12 changes: 10 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,18 @@
/*
* TODO #1 Закончить определение класса ProductEventArgs
*/
class ProductEventArgs
class ProductEventArgs : EventArgs
{
/*
* TODO #2 Добавить определение необходимых компонент
* класса ProductEventArgs
*/
public string Old { get; }
public string Current { get; }
public ProductEventArgs(string old, string current)
{
Old = old;
Current = current;
}
}
}
19 changes: 16 additions & 3 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,31 @@ internal Product Product
static void Main(string[] args)
{
Product product = new Product("Some product name", 0);

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

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

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

private static void OnProductNameChanged(object sender, ProductEventArgs e)
{
Console.WriteLine("The name of the product was changed from '{0}' to '{1}'", e.Old, e.Current);
}
private static void OnProductPriceChanged(object sender, ProductEventArgs e)
{
Console.WriteLine("The price of the product was changed from '{0}' to '{1}'", e.Old, e.Current);
}
}
}