I have a class with two properties: weigh and Id. I want to trigger an event when weigh is set. The event to be triggered is a messageBox showing the Id property. The Messagebox is shown, but it doesn't include the Id property.
Here's the entire code: https://pastebin.com/zpHn48gL
public class MyClass
{
//Custom type Event declaration
public event EventHandler<Mas4TEventArgs> Info;
decimal _weigh;
//properties
public string Id { get; set; }
public decimal Weigh
{
get { return this._weigh; }
set //When this property is changed, invoke Info Event, passing Id property to be shown on messagebox.
{
this._weigh= value;
Info?.Invoke(this, new Mas4TEventArgs(this.Id));
}
}
}
public class Mas4TEventArgs : EventArgs
{
//constructor
public Mas4TEventArgs(string pId) { IdArgu = pId; }
//property IdArgu
public string IdArgu { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyClass C = new MyClass();
//suscription
C.Info += C_Info;
//Function to be triggered by the event
void C_Info(object sendr, Mas4TEventArgs ev)
{
try
{ //ev.IdArgu doesn't show on the messagebox.
MessageBox.Show("Evento consumido. " + " Id: " + ev.IdArgu);
}
catch (Exception) { }
}
//properties
C.Weigh = Convert.ToDecimal(textBox1.Text);
C.Id = TxtId.Text;
//just to check the two properties have been parsed correctly.This works as intended.
MessageBox.Show("Ingresado: Peso: " + C.Peso.ToString() + " Id: " + C.Id);
}
}
or, if you prefer this: https://lpaste.net/3710707699130826752