| View previous topic :: View next topic |
| Author |
Message |
David Freeman Guest
|
Posted: Sat Dec 10, 2005 6:05 pm Post subject: newb? updating class's parent |
|
|
I have the following class structure:
Tmoney = class
private
fvalue: double;
function getvalue: double read getvalue write setvalue
procedure setvalue(const value: double);
public
property value: double read getvalue write setvalue
end;
TLineItem = class
private
fischanged: boolean;
flinevalue: tmoney;
public
function getischanged: boolean;
function getlinevalue: double;
procedure setischanged(const value: boolean);
procedure setlinevalue(const value: double);
public
property ischanged: boolean read getischanged write setischanged;
property linevalue: double read getlinevalue write setlinevalue;
end;
In my code I have
thislineitem.ischanged := false;
thislineitem.linevalue.value := 0.00;
and later
calculations
thislineitem.linevalue.value := calculatedvalue;
I want thislineitem.ischanged set true here.
When flinevalue was a double, I could set fischanged := true in
..setlinevalue. However, as with flinevalue as a tmoney, .setlinvalue is not
used.
Is there any way of setting ischanged 'automatically' or do I just need to
remember to set it in code each time?
Note: tmoney is used in numerous other classes, only a few of which have
fischanged.
Note: Delphi 7 professional (still)
Thanks
David
|
|
| Back to top |
|
 |
Lee_Nover Guest
|
Posted: Sat Dec 10, 2005 6:35 pm Post subject: Re: newb? updating class's parent |
|
|
| Quote: | Is there any way of setting ischanged 'automatically' or do I just need to
remember to set it in code each time?
|
you can use the Observer pattern
I use Joanna's implementation (explained here: http://blogs.teamb.com/joannacarter/articles/690.aspx)
|
|
| Back to top |
|
 |
Bob Dawson Guest
|
Posted: Sat Dec 10, 2005 8:48 pm Post subject: Re: newb? updating class's parent |
|
|
"David Freeman" wrote
| Quote: | I have the following class structure:
Tmoney = class
private
fvalue: double;
function getvalue: double read getvalue write setvalue
//---seriously doubt above line  |
// I'll assume that these are not real code.
| Quote: | procedure setvalue(const value: double);
public
property value: double read getvalue write setvalue
end;
|
Don't create a new TMoney object but then leave responsibility for knowing
its status with the line item.
First, make TMoney track its own IsChanged value, then
ajust TLineItem to make GetIsChanged check its component parts. For now,
that would be
function TLineItem.GetIsChanged: boolean;
begin
Result := FIsChanged or FLineValue.IsChanged;
end;
| Quote: | Is there any way of setting ischanged 'automatically' or do I just need to
remember to set it in code each time?
|
It ends up still getting set automatically, but at the TMoney level.
bobD
|
|
| Back to top |
|
 |
Bob Dawson Guest
|
Posted: Sat Dec 10, 2005 8:59 pm Post subject: Re: newb? updating class's parent |
|
|
"Lee_Nover" wrote
you can use the Observer pattern
Considerable overkill here, I think. If TLineItem needs to know if FMoney is
changed, it can just ask.
bobD
|
|
| Back to top |
|
 |
David Freeman Guest
|
Posted: Sat Dec 10, 2005 10:02 pm Post subject: Re: newb? updating class's parent |
|
|
Thanks guys, I think I will go with Bob's proposal, certainly simpler on the
surface and definitely adequate for what I want.
Thanks
David
|
|
| Back to top |
|
 |
preston Guest
|
Posted: Mon Dec 12, 2005 3:10 pm Post subject: Re: newb? updating class's parent |
|
|
Bob Dawson wrote:
| Quote: | "Lee_Nover" wrote
you can use the Observer pattern
Considerable overkill here, I think.
bobD
|
Swinging a big hammer helps to keep ones body and mind in shape ;)
|
|
| Back to top |
|
 |
|