none
How to monitor the change of a variable's value? RRS feed

  • Question

  • Hi everybody,

    I've a C# class who contains a integer variable: 

    public class AClassController
    {
      public static int x;
      ...
      ...
    
      public void methodWhoChangeXValue()
      {
      ...
      x = //newIntValue;
      ...
      }
    }

    Furthermore, I've a Form class who contains a multiline TextBox who must print the x value everytime it's value change, after a button click:

    public partial class AForm : Form
    {
      public AForm()
      {
        InitializeComponent();
        ...
      }
      ...
    
     private void button_Click(object sender, EventArgs e)
     {           
      //Everytime the x value change, there must be a new line in 
      the multiline textbox             
       textBoxMultiline.AppendText(AClassController.x.ToString() 
      + Environment.NewLine);
    }

    How I can add a new line in the textbox when the  methodWhoChangeXValue() changes the X value? Note: the methodWhoChangeXValue() is called in loop in a AClassController's method, so the value of x often changes.

    Can you help me?

    Thank you

    Thursday, March 26, 2020 1:01 PM

All replies

  • If x is changed by “=” only, then consider this modification:

    public class AClassController
    {
       static int xv;
       public static event EventHandler XChanged;
     
       public static int x
       {
          get
          {
             return xv;
          }
          set
          {
             if( xv != value )
             {
                xv = value;
                XChanged?.Invoke( null, null );
             }
          }
       }
    . . .

     

    The possible usage in the form:

     

    AClassController.XChanged += (s,a) => textBoxMultiline.AppendText(AClassController.x + Environment.NewLine);



    Thursday, March 26, 2020 3:27 PM
  • Hi,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 31, 2020 9:16 AM