Depending on what you want to do, you can implement an observer pattern. I think FPC supports this since version 2.6.4
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.
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;
@Weitentaaal: I've held off commenting up to this point, since I was hoping that either you or somebody else would throw light on the fundamental question: when you say "variable", do you mean a "classic Pascal" unmanaged integer (etc.) in memory, rather than a field of a class?
My suspicion is that if you do, neither FPC nor any other conventional language has the capability of flagging a state-change without wrapping the variable in something: either compile-time complexity (the variable becomes a field in an object, and is then accessed via a property and a setter) or run-time complexity probably involving either an interpretive runtime or alternatively processor-specific debugging capabilities.
MarkMLl
i ment classic Pascal Variable (Integer, String, boolean etc)