Solved

Explenation about some declared variables.

Posted on 2009-07-03
2
156 Views
Last Modified: 2012-05-07
Dear experts,

I have found an example on EE how to load and save treeview to and from a database.
I tried to understand the code so i have a couple of questions about the declared variables.
I have put a part of the code in the Code-section.
Who can explain it to me what the following variables are for:
=> fStatus: byte
=> fSaveImmediate: boolean;
 => fIsNew: Boolean;
=> fSaver: TAdoQuery;
type
  TItem = class(TObject)
  private
    fStatus: byte;
    fParentID: integer;
    fNodeText: string;
    fID: integer;
    fData: string;
    fSaver: TAdoQuery;
    fChanged: Boolean;
    fSaveImmediate: boolean;
    fIsNew: Boolean;
    fImageIndex: integer;
    procedure SetData(const Value: string);
    procedure SetNodeText(const Value: string);
    procedure SetParentID(const Value: integer);
    procedure SetStatus(const Value: byte);
    procedure SetImageIndex(const Value: integer);
  protected
    procedure Changed; dynamic;
  public
    constructor Create(aSaver: TAdoQuery; AId, AParentID: integer; AStatus: byte);
    constructor LoadFromDataset(aSaver: TAdoQuery);
    procedure Save;
    procedure Delete;
    procedure BeginUpdate;
    procedure EndUpdate;
  published
    property ID: integer read fID write fID;
    property ParentID: integer read fParentID write SetParentID;
    property ImageIndex: integer read fImageIndex write SetImageIndex;
    property Status: byte read FStatus write SetStatus;
    property Data: string read fData write SetData;
    property NodeText: string read fNodeText write SetNodeText;
  end;
 
 
 
{ TItem }
 
procedure TItem.BeginUpdate;
begin
  fSaveImmediate := false;
end;
 
procedure TItem.Changed;
begin
  fChanged := True;
  if fSaveImmediate then Save;
end;
 
constructor TItem.Create(aSaver: TAdoQuery; AId, AParentID: integer;
  AStatus: byte);
begin
  inherited Create;
  fChanged := False;
  fSaveImmediate := True;
  fIsNew := True;
  fSaver := aSaver;
  fID := AID;
  fParentID := AParentID;
  fStatus := AStatus;
  fNodeText := '';
  fData := '';
end;
 
procedure TItem.Delete;
begin
  //
end;
 
procedure TItem.EndUpdate;
begin
  fSaveImmediate := True;
  if fChanged then
    Save;
end;
 
constructor TItem.LoadFromDataset(aSaver: TAdoQuery);
begin
  Create(aSaver, 0, 0, 0);
  if not aSaver.IsEmpty then
  begin
    fId := aSaver.FieldByName('ID').AsInteger;
    fParentID := aSaver.FieldByName('PARENT').AsInteger;
    fStatus := aSaver.FieldByName('STATUS').AsInteger;
    fNodeText := aSaver.FieldByName('NAME').AsString;
    if aSaver.FindField('DATA') <> nil then
      fData := aSaver.FieldByName('DATA').AsString;
    fImageIndex := aSaver.FieldByName('IMAGE_INDEX').AsInteger;
    fIsNew := False;
  end;
end;
 
procedure TItem.Save;
begin
  if fIsNew then
    fSaver.SQL.Text :=
      'insert into tree (parent, name, status, image_index, data) values (:v1, :v2, :v3, :v4, :v5)'
  else
    fSaver.SQL.Text :=
      'update tree set parent = :v1, name = :v2, status = :v3, image_index = :v4, data = :v5 where id = :v6';
  fSaver.Parameters.ParamByName('v1').Value := fparentID;
  fSaver.Parameters.ParamByName('v2').Value := fNodeText;
  fSaver.Parameters.ParamByName('v3').Value := fStatus;
  fSaver.Parameters.ParamByName('v4').Value := fImageIndex;
  fSaver.Parameters.ParamByName('v5').Value := fData;
  if not fIsNew then
    fSaver.Parameters.ParamByName('v6').Value := fId;
  fSaver.ExecSQL;
  if fIsNew then
  begin
    fSaver.SQL.Text :=
      'SELECT id from tree order by id desc';
    fSaver.Open;
    fId := fSaver.FieldByName('ID').asInteger;
    fSaver.Close;
    fIsNew := False;
  end;
  fChanged := False;
end;
 
procedure TItem.SetData(const Value: string);
begin
  if fData <> Value then
  begin
    fData := Value;
    Changed;
  end;
end;
 
procedure TItem.SetImageIndex(const Value: integer);
begin
  if fImageIndex <> Value then
  begin
    fImageIndex := Value;
    Changed;
  end;
end;
 
procedure TItem.SetNodeText(const Value: string);
begin
  if fNodeText <> Value then
  begin
    fNodeText := Value;
    Changed;
  end;
end;
 
procedure TItem.SetParentID(const Value: integer);
begin
  if fParentId <> Value then
  begin
    fParentID := Value;
    Changed;
  end;
end;
 
 
procedure TItem.SetStatus(const Value: byte);
begin
  if fStatus <> Value then
  begin
    FStatus := Value;
    Changed;
  end;
end;
 
end.

Open in new window

0
Comment
Question by:peterkiers
2 Comments
 
LVL 11

Accepted Solution

by:
dougaug earned 500 total points
ID: 24772964
Follow a possible explanation:

fStatus - variable used to store the value of property Status obtained from the database or when the user modify the subitem before it's updated in the database

fSaveImmediate - controls if the treeview item is saved in the database immediately after a change in any one of its subitems. If more than one subitem (or database field) will be changed, the method BeginUpdate should be call first the modifications to avoid one update for each field changed. After all subitems of a treeview item have been changed, the method EndUpdate must be called to update the database with the modifications in just one access.

fIsNew - check if the treeview item is new (not exists in database) and based on it takes different actions: insert a new record in database or update an existing

fSaver - TAdoQuery object instance that is used to execute commands in the database (update, insert, select)

Hope this help you!

Regards
0
 
LVL 1

Author Comment

by:peterkiers
ID: 24773078
thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 104
HTML text in the body of an email (delphi code) 12 207
Is anyone willing to have a look through this code and help debug? 25 91
RESTRequest Parameter 4 51
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question