Save values ​​from beforepost event for afterpost event

I am writing this question for Delphi 2007, but I am sure that this is a common problem in all languages.

So, I have a project where I need to store information about the old and new values ​​of certain fields (which are specified in the BeforePost event of the dataset I'm working with) and use them in the AfterPost event.

I am currently using global variables, but there are already so many in the project that this becomes a real problem when it comes to managing documentation and / or comments.

Basically, I ask if there is a better way (in Delphi 2007 or in general) to save information from the BeforePost event of the dataset and return them to the AfterPost event.

+7
events delphi delphi-2007
source share
1 answer

first create a new custom data source

TDataRecord = array of record FieldName: string; FieldValue: Variant; end; TMyDataSource = class(TDataSource) private LastValues: TDataRecord; procedure MyDataSourceBeforePost(DataSet: TDataSet); procedure SetDataSet(const Value: TDataSet); function GetDataSet: TDataSet; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; function GetLastValue(FieldName: string): Variant; property MyDataSet: TDataSet read GetDataSet write SetDataSet; end; { TMyDataSource } constructor TMyDataSource.Create(AOwner: TComponent); begin inherited Create(AOwner); end; destructor TMyDataSource.Destroy; begin SetLength(LastValues, 0); inherited Destroy; end; function TMyDataSource.GetDataSet: TDataSet; begin Result := DataSet; end; procedure TMyDataSource.SetDataSet(const Value: TDataSet); begin DataSet := Value; DataSet.BeforePost := MyDataSourceBeforePost; end; procedure TMyDataSource.MyDataSourceBeforePost(DataSet: TDataSet); var i: integer; begin SetLength(LastValues, DataSet.FieldCount); for i:=0 to DataSet.FieldCount-1 do begin LastValues[i].FieldName := DataSet.Fields.Fields[i].FieldName; LastValues[i].FieldValue := DataSet.Fields.Fields[i].OldValue; end; end; function TMyDataSource.GetLastValue(FieldName: string): Variant; var i: integer; begin Result := Null; for i:=0 to Length(LastValues)-1 do if SameText(FieldName, LastValues[i].FieldName) then begin Result := LastValues[i].FieldValue; break; end; end; 

and after overriding the data source application

  TForm1 = class(TForm) private MyDataSource: TMyDataSource; end; procedure TForm1.FormCreate(Sender: TObject); begin ADOQuery1.Active := true; MyDataSource := TMyDataSource.Create(Self); MyDataSource.MyDataSet := ADOQuery1; DBGrid1.DataSource := MyDataSource; end; procedure TForm1.FormDestroy(Sender: TObject); begin MyDataSource.Free; end; procedure TForm1.ADOQuery1AfterPost(DataSet: TDataSet); var AValue: Variant; begin AValue := MyDataSource.GetLastValue('cname'); if not VarIsNull(AValue) then; end; 
0
source share

All Articles