Depending on what you want to do, you can implement an observer pattern. I think FPC supports this since version 2.6.4
I think thats an overkill for a Single Variable ?
Outside of a class you can use a stand-alone property.
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Forms, Dialogs, Spin;
type
TForm1 = class(TForm)
AlertSpinEdit: TSpinEdit;
procedure AlertSpinEditChange(Sender: TObject);
end;
procedure SetAlertIfChanged(aValue: Integer);
function GetAlertIfChanged: Integer;
procedure Alert;
property AlertIfChanged: Integer read GetAlertIfChanged write SetAlertIfChanged;
var
Form1: TForm1;
implementation
var
alertInt: Integer = 50;
procedure SetAlertIfChanged(aValue: Integer);
begin
if aValue <> alertInt then begin
alertInt := aValue;
Alert;
end;
end;
function GetAlertIfChanged: Integer;
begin
Exit(alertInt);
end;
procedure Alert;
begin
ShowMessageFmt('AlertIfChanged is now %d',[AlertIfChanged]);
end;
{$R *.lfm}
{ TForm1 }
procedure TForm1.AlertSpinEditChange(Sender: TObject);
begin
AlertIfChanged := (Sender as TSpinEdit).Value;
end;
end.
Isn't this the same when i use a property for a Variable like that (pseudo Code):
TMyClass = class
private
int fx;
procedure SetX(aValue: Integer);
function GetX: Integer;
procedure xChanged
public
property x:Integer read Getx write SetX;
end;
implementation
procedure TMyClass .SetX(AVAlue: Integer);
begin
fx:= AVAlue;
xChanged();
end;
procedure TMyClass GetX(): Integer;
begin
Result:= fX;
end;
i hope u get what i was trying to say
Flag - is obvious solution:
procedure TMyClass.SetValue(AValue:TValue);//Setter
begin
if AValue <> FValue then begin
FValue := AValue;
FValueChanged := True;//Flag
OnValueChanged(Self, AValue);//Or event
end;
end;
Isn't this the same as above too ?
Btw. i need to fire an event just like TEdit has OnChange.
I did not want to create a property and use its setter / getter for this, or create a whole new value just to store the old value. Something like i assign a Handler to a Value and if the Value is changing the Handler "handles" the new Value.
Thanks anyway for your suggestions