Solved

Explenation about some declared variables.

Posted on 2009-07-03
2
148 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now