detect if a variable has Changed lazarus / 62-General ---------------------------------------------------------------------- Weitentaaal · 2021-10-19 09:19 Hello , is this Somehow possible ? I know i can use Getter and Setter or define an old Var to store old Value. Is there a better approach ? Thanks in advance ---------------------------------------------------------------------- Mr.Madguy · 2021-10-19 09:37 Flag - is obvious solution: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyClass.SetValue(AValue:TValue);//Setterbegin  if AValue <> FValue then begin    FValue := AValue;    FValueChanged := True;//Flag    OnValueChanged(Self, AValue);//Or event  end;end; ---------------------------------------------------------------------- howardpc · 2021-10-19 09:45 Outside of a class you can use a stand-alone property. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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. ---------------------------------------------------------------------- white_zombie · 2021-10-19 10:25 Depending on what you want to do, you can implement an observer pattern. I think FPC supports this since version 2.6.4 ---------------------------------------------------------------------- Weitentaaal · 2021-10-19 11:14 Quote from: white_zombie on October 19, 2021, 12:25:05 pmDepending 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 ? Quote from: howardpc on October 19, 2021, 11:45:33 amOutside of a class you can use a stand-alone property. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  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  Quote from: Mr.Madguy on October 19, 2021, 11:37:00 amFlag - is obvious solution: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyClass.SetValue(AValue:TValue);//Setterbegin  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 ---------------------------------------------------------------------- MarkMLl · 2021-10-19 11:21 @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 ---------------------------------------------------------------------- Weitentaaal · 2021-10-19 11:46 Quote from: MarkMLl on October 19, 2021, 01:21:11 pm@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 Sorry my fault, i ment classic Pascal Variable (Integer, String, boolean etc)  in the following example i have a "Variable" : "fX". if "fX" is changing i want to fire an event. Thats why  i created Property "X" to use the getter and setter of "X". This way i can fire an Event ("xChanged") in the setter methode Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  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   if AValue <> fx then begin //forgott this      fx:= AVAlue;      xChanged();   end;end;  procedure TMyClass GetX(): Integer;begin   Result:= fX;end; ---------------------------------------------------------------------- balazsszekely · 2021-10-19 12:29 @Weitentaaal Try something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit uDetectChange; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils; type     { TDetectChange }  TOnVariableChanged = procedure(Sender: TObject; AX: Integer) of object;  TDetectChange = class  private    FX: Integer;    FOnVariableChanged: TOnVariableChanged;    procedure SetX(aValue: Integer);  public    constructor Create(AX: Integer);    destructor Destroy; override;  public    property X: Integer read FX write SetX;    property OnVariableChanged: TOnVariableChanged read FOnVariableChanged write FOnVariableChanged;  end; implementation constructor TDetectChange.Create(AX: Integer);begin  FX := AX;end; destructor TDetectChange.Destroy;begin  //  inherited;end; procedure TDetectChange.SetX(aValue: Integer);begin  if AValue <> FX then  begin    FX := AValue;    if Assigned(FOnVariableChanged) then      FOnVariableChanged(Self, FX);  end;end; end.  then: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,  uDetectChange; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private   FDetectChange: TDetectChange;   procedure OnVariableChange(Sender: TObject; AX: Integer);  public   end; var  Form1: TForm1; implementation{$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  FDetectChange := TDetectChange.Create(3);  FDetectChange.OnVariableChanged := @OnVariableChange;end; procedure TForm1.FormDestroy(Sender: TObject);begin  FDetectChange.Free;end; procedure TForm1.OnVariableChange(Sender: TObject; AX: Integer);begin  ShowMessage('Variable X has changed, new value: ' + IntToStr(AX));end; procedure TForm1.Button1Click(Sender: TObject);begin  FDetectChange.X := 15;end;  end. ---------------------------------------------------------------------- MarkMLl · 2021-10-19 12:32 Quote from: Weitentaaal on October 19, 2021, 01:46:07 pmi ment classic Pascal Variable (Integer, String, boolean etc) I thought you did, hence that it was necessary (eventually) to ask for clarification. So what you're effectively doing there is what I referred to as a compile-time wrapper... I don't know if any sort of runtime wrapper using e.g. debugging registers could be implemented portably and that sort of thing has notoriously poor OS support. MarkMLl p.s. That callback should probably be protected by if Assigned(). ---------------------------------------------------------------------- Weitentaaal · 2021-10-19 13:23 "p.s. That callback should probably be protected by if Assigned()." Thanks for the Hint. Thanks to all of your Reply's, i guess i will keep the Properties.