I need help with this exercise
/*
Defina el delegado PrecioCambiadoEventHandler, la clase PrecioCambiadoEventArgs y la clase Articulo
de tal forma que el programa siguiente genere la siguiente salida por consola
Articulo 1 valia 0 y ahora vale 10
Articulo 1 valia 10 y ahora vale 12
Articulo 1 valia 12 y ahora vale 14
*/
using System;
class Program {
public static void Main(string[] args) {
Articulo a = new Articulo();
a.PrecioCambiado += new PrecioCambiadoEventHandler(precioCambiado);
a.Codigo = 1;
a.Precio = 10;
a.Precio = 12;
a.Precio = 12;
a.Precio = 14;
Console.ReadKey(true);
}
public static void precioCambiado(object sender, PrecioCambiadoEventArgs e){
string texto = "Artículo {0} valía {1} y ahora vale {2}";
Console.WriteLine(texto,e.Codigo,e.PrecioAnterior,e.PrecioNuevo);
}
}
The Article class has the Read / Write Code and Price properties. It also has the PriceChanged event that occurs when the value of the Price property is changed (note that if the same value is assigned, the event does not occur).
I did this from below. but I do not understand delegates well
class PrecioCambiadoEventArgs:EventArgs {
public int Codigo{
get{return Codigo;}
set{Codigo = value;}
}
public double PrecioAnterior{
get{return PrecioAnterior;}
set{PrecioAnterior = value;}
}
public double PrecioNuevo{
get{return PrecioNuevo;}
set{PrecioNuevo = value;}
}
public PrecioCambiadoEventArgs(int codigo, double precioAnterior, double precioNuevo){
this.Codigo = codigo;
this.PrecioAnterior = precioAnterior;
this.PrecioNuevo = precioNuevo;
}
}
class Articulo {
/* posee el evento PrecioCambiado que se produce cuando se cambia el valor de la propiedad Precio
(observe que si se asigna el mismo valor el evento no se produce).*/
private int cod;
private double pre;
public Articulo(){
}
public void PrecioCambiadoEventHandler PrecioCambiado (object sender, PrecioCambiadoEventArgs e) {
}
public int Codigo{
get{
return cod;
}
set{
cod = value;
}
}
public double Precio{
get{
return pre;
}
set{
double pre = value;
//Llamar al Evento aca?
}
}
}