I am using the follow code in Embarcadero C++Builder XE8.
In Unit1.h:
private:
void SetValue (Currency value);
Currency FValue;
public:
__property Currency Value = {write= SetValue , read=FValue};
In Unit1.cpp:
void TForm1::SetValue(Currency _Value)
{
FValue= _Value;
Label1->Caption= _Value;
}
In the main program when I call:
Value = 10; // it calls SetValue and the Value will be set to 10
Value+= 10; // it does not call SetValue and Value will not be set to 20
Why does Value+= 10
not call SetValue(20)
?
You cannot use compound assignment operators with properties. The compiler does not implement that, and never has.
Value = 10;
is a direct assignment, so it invokes the property's setter, as expected.Value += 10;
does not translate toValue = Value + 10;
like you are expecting. It actually invokes the property's getter to retrieve a temporary value, and then increments and assigns the temporary to itself. The temporary is never assigned back to the property, which is why the property's setter is not called. In other words, it translates totemp = Value; temp += 10;
.To do what you are attempting, you must explicitly use the
+
and=
operators separately: