Delegates C # help

0

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?
    }
}
}
    
asked by Nazareno Khan 12.07.2017 в 02:26
source

2 answers

0

You should use the INotifyPropertyChanged interface.

More info: link

Here is a pretty clear example: link

    
answered by 12.07.2017 в 19:09
0

Yes. what you are thinking is the right thing to do.

You must generate an object that contains the parameters, and then call the event exactly where you think it is. Something like:

PrecioCambiadoEventArgs p = new PrecioCambiadoEventArgs(Codigo, pre, value)

and then call the event

PrecioCambiado (this, p)

Remember that as he says there, you have to be careful not to call him if the price does not really change.

Equal the problem that I see, is that at no time you are defining an event. Without a handler for the same, but and the event?

    
answered by 12.07.2017 в 19:25