Alessandro Gibbi
Alessandro Gibbi

Reputation: 1

c# unexpected change of value - beware the debugger

C# on Visual studio 2017.

I created my class and I instanced an object of that class. In the class there are only two private value fields with two propeties for access to them and one private flag (bool). The problem is: the set accessor of a property only touch his (only logically) related field and the flag, but at execution time when I "call" the set accessor of that property ALL the fields are affected.

In my implementation I have only a form with only a button: the clik of the button increases the value of the first field using his property.

My code.

public class DriveMeCrazyClass
{        
    public double _num; // a number
    public double _dou; // his double        
    public bool dou_isValid; // I'll really update the field value only on a request.

    // The idea is: 
    // _num e _dou are related, so when _num changes 
    //    I should calculate the new value for _dou.
    // BUT
    // I prefer to postpone the calculation 
    //    to the moment when I will be asked for the value
    // SO
    // I only "take note" of this fact on the flag dou_isValid

    public DriveMeCrazyClass()
    {
        _num = _dou = 0;
        dou_isValid = true;
    }

    public event EventHandler DMCCchanged;
    public double Num
    {
        get { return _num; }

        set
        {
            if (_num == value) return;
            _num = value;                
            dou_isValid = false;                

            DMCCchanged?.Invoke(this, EventArgs.Empty);
        }
    }

    public double Dou
    {
        get
        {
            if (dou_isValid) return _dou;
            dou_isValid = true;
            DMCCchanged?.Invoke(this, EventArgs.Empty);
            return _dou = 2 * _num;
        }
        set
        {                
            // ..... no matter now
        }
    }
}




public partial class Form1 : Form
{        
    public DriveMeCrazyClass aa = new DriveMeCrazyClass();        

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        aa.DMCCchanged += new System.EventHandler(aa_changed);
        lblNum.Text = "0";
        lblDou.Text = "0";

    }

    private void Num_inc_Click(object sender, EventArgs e)
    {
        aa.Num++;
    }

    private void aa_changed(object sender, EventArgs e)
    {
        lblNum.Text = aa._num.ToString();
        lblDou.Text = aa._dou.ToString();
    }
}

It's like as the compiler calls the get accessor of Dou property. I don't know why. An optimization? Or what? Really, I don't get it. Can anybody help me?

*****EDIT******

I put on a new code, slightly changed. _num and _dou are now public and I added two label to show their values - not using the accessors Num e Dou.

without debug: it works great!

BUT

if I put a breakpoint on Num.set and I keep going step by step - WATCHING Num and Dou values - it's a mess!! The debugger call the accessor Dou.get and update _dou value and set true the flag dou_isValid. I know, I handle it.

OK, it's clear now! Thx Robin, Thx Camilo, Thx to all, really.

By the way... This problem is a part of a larger one, concerning bindig. I hope to be able to resolve the main problem now, otherwise.... (this is a threat for your free time :) )

Upvotes: 0

Views: 252

Answers (1)

Robin Bennett
Robin Bennett

Reputation: 3231

It's like as the compiler calls the get accessor of Dou property. I don't know why.

Not the compiler, but the debugger. When you look at the properties of your class, it gets the values of all the properties, which runs the code.

Methods are not run by the debugger, so this would work:

double GetDou()
{
    if (dou_isValid) return _dou;
    dou_isValid = true;
    return _dou = 3 * _num;
}

Upvotes: 4

Related Questions