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
47 changes: 17 additions & 30 deletions Product.cs
Original file line number Diff line number Diff line change
@@ -1,67 +1,54 @@
using System;
using System;
using System.Security.Policy;

namespace Events
{
/// <summary>
/// Класс должен описывать представление о товаре.
/// В рамках лабораторной работы должен являться
/// источником события
/// </summary>
class Product
{

#region Variables
/// <summary>
/// Наименование
/// </summary>

private string name;
/// <summary>
/// Стоимость
/// </summary>
private decimal price;

#endregion

#region Properties

/// <summary>
/// Наименование
/// </summary>
public string Name
{
get { return name; }
set
{
var args = new ProductEventArgs<string>(name,value);
name = value;
/*
* TODO #4 Инициировать уведомление об
* изменении наименования
*/
if (NameChanged != null)
{
NameChanged(this, args);
}
}

}
/// <summary>
/// Стоимость
/// </summary>

public decimal Price
{
get { return price; }
set
{
var args = new ProductEventArgs<decimal>(price,value);
price = value;
/*
* TODO #5 Инициировать уведомление об
* изменении стоимости
*/
if (PriceChanged != null)
{
PriceChanged(this, args);
}
}
}

#endregion

#region Events

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

#endregion

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

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

public ProductEventArgs( T oldvalue, T newvalue)
{
OldValue = oldvalue;
NewValue = newvalue;
}

}
}
}
61 changes: 33 additions & 28 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -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<decimal> 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<string> e)
{
Console.WriteLine("Товар '{0}' сменил имя на '{1}'", e.OldValue, e.NewValue);
}
}
}