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
14 changes: 13 additions & 1 deletion Product.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace Events
{
Expand Down Expand Up @@ -32,11 +32,16 @@ public string Name
get { return name; }
set
{
ProductEventArgs arg = new ProductEventArgs(name);
name = value;
/*
* TODO #4 Инициировать уведомление об
* изменении наименования
*/
if (nameChanged != null)
{
nameChanged(this, arg);
}
}
}
/// <summary>
Expand All @@ -47,11 +52,16 @@ public decimal Price
get { return price; }
set
{
ProductEventArgs arg = new ProductEventArgs(price);
price = value;
/*
* TODO #5 Инициировать уведомление об
* изменении стоимости
*/
if (priceChanged != null)
{
priceChanged(this, arg);
}
}
}

Expand All @@ -62,6 +72,8 @@ public decimal Price
/*
* TODO #3 Добавить определение событий
*/
public event EventHandler<ProductEventArgs> nameChanged;
public event EventHandler<ProductEventArgs> priceChanged;

#endregion

Expand Down
18 changes: 16 additions & 2 deletions ProductEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Events
using System;

namespace Events
{
/// <summary>
/// Класс, который служит для передачи аргументов
Expand All @@ -8,11 +10,23 @@
/*
* TODO #1 Закончить определение класса ProductEventArgs
*/
class ProductEventArgs
class ProductEventArgs : EventArgs
{
/*
* TODO #2 Добавить определение необходимых компонент
* класса ProductEventArgs
*/
public string oldName { get; }
public decimal oldPrice { get; }

public ProductEventArgs(string oldName)
{
this.oldName = oldName;
}

public ProductEventArgs(decimal oldPrice)
{
this.oldPrice = oldPrice;
}
}
}
22 changes: 20 additions & 2 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,36 @@ 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 = "NewNewNew";
product.Price = 1000;
}

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

private static void onNameChanged(object sender, ProductEventArgs e)
{
Product product = sender as Product;
Console.WriteLine("Name has been changed from " + e.oldName + " to " + product.Name);
}

private static void onPriceChanged(object sender, ProductEventArgs e)
{
Product product = sender as Product;
Console.WriteLine("Price has been changed from " + e.oldPrice + " to " + product.Price);
}

}
}