1

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

Richardissimo
  • 5,596
  • 2
  • 18
  • 36
Destroyer
  • 13
  • 2
  • First of all writing code in class model is not a good practice @Destroyer – Hitesh Anshani Jun 24 '18 at 00:00
  • Refer this maybe its works fir you https://www.codeproject.com/Articles/30732/event-and-custom-eventargs-in-depth-for-beginners – Hitesh Anshani Jun 24 '18 at 00:16
  • @D-john Anshani thanks for the advice. Aparently I'm doing exactly the same as the link you suggested. The only part that differs is that on the example the suscriber uses an overrided method, and I'm trying to use a setter. So I stil can't make it work. – Destroyer Jun 24 '18 at 01:18
  • Please ignore last MessageBox where it says C.Peso ...Replace Peso for Weigh.It is not the issue on the code. – Destroyer Jun 24 '18 at 03:17

1 Answers1

0

Your code includes the following key lines...

C.Weigh = Convert.ToDecimal(textBox1.Text);
C.Id = TxtId.Text;

Notice that the Id is being set after the Weigh. So at the point where the Weigh value is being set, it will raise an event with whatever was in Id before you set Id on the next line.

Swap the two lines over so that Id is set first, and your code will behave as you are expecting.

Richardissimo
  • 5,596
  • 2
  • 18
  • 36
  • @Destroyer Well, I have your code from pastebin, and when I do `C.Id = TxtId.Text;` before `C.Weigh = Convert.ToDecimal(textBox1.Text);` with "MyId" in TxtId.Text, the messagebox appears saying `Evento consumido. Id: MyId`. So you're going to need to edit your question to show what your code looks like now, or what the problem is now; because this fixes it for me. – Richardissimo Jun 24 '18 at 19:58
  • @Destroyer I'm glad this has sorted it out for you. As this was your first question, I'd like to offer one final thought: When someone has volunteered their time to read your question, download your code, diagnose the problem, and write an explanation of the problem which requires you to simply swap two lines over, it's a little disrespectful to say "I tried that" when you clearly didn't. And if I hadn't been patient enough to nudge you to actually try it, you could still be waiting for an answer; because it would have been difficult to offer an alternative answer. – Richardissimo Jun 26 '18 at 21:26