Examples Delphi

USING THE CREATE AND DESTROY METHODS OF A DIY OBJECT
[see second example for OBJECT ORIENTED 'DBHANDLER'
IMPLEMENTATION]
******************************************
first example
FIRST DECLARE YOUR OBJECT WITH THESE METHODS, LIKE SO,
and note that the TBitMap object declared as part of
the object here must itself be created in the object's
constructor and destroyed in the object's destructor.
Note also the 'override' statement in the function header
for the Destroy method (which ensures that Delphi will
do any other cleanup necessary after we've done our
explicit bits of cleaning up)
type
TCNSVDraw = class(TObject)
constructor Create;
destructor Destroy; override;
procedure FillTBitMapDrawArea(thisCanvas: TCanvas;
cWidth, cHeight: Integer);
procedure DisplayMatrixToScreen(theCanvas: TCanvas);
procedure ReDrawWFrameAndGrid(theCanvas: TCanvas; cWidth, cHeight: Integer);
private
memBitMap: Graphics.TBitMap;
public
end;
constructor TCNSVDraw.Create;
begin
memBitMap := Graphics.TBitmap.Create;
memBitMap.Width := Screen.Width;
memBitMap.Height := Screen.Height;
end;
destructor TCNSVDraw.Destroy;
begin
memBitMap.Free;
end;
Note that if we didn't need to free up bitmap resources within our own
DIY destructor then we could still say CNSVDraw.Free somewhere WITHOUT
having explicitly declared any kind of destructor method as part of
our object (so that CNSVDraw.Free would call the DEFAULT destructor).
******************************************
Now, if the object above is declared within it's own unit, then
we will probably want to reference (ie use) it from elsewhere,
so we can set up a variable to instantiate the object like so
type
TMainForm = class(TForm)
private
{private declarations...}
public
{public declarations...}
end;
var
CNSVDraw : TCNSVDraw;
implementation
{$R *.DFM}
uses
DrawUnit;
***var
***we COULD ALSO put the
***CNSVDraw : TCNSVDraw;
***statement [above] here instead
so now we can call the object's constructor from the main form's
OnCreate, and the object's destructor from the main form's OnDestroy
procedure TMainForm.FormCreate(Sender: TObject);
begin
CNSVDraw := TCNSVDraw.Create;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
{as we destroy the EditStyle form, (which is 'autocreated' once at the
beginning) so we also call the destructor of the 'Drawing Style' object
we have been calling upon to implement drawing functionality since the
form was created...}
CNSVDraw.Free;
end;
******************************************
Final note:
Above, it was pointed out that we had the choice of placing the VARIABLE
declaration of the object in two different places in the (second) unit
that calls our object's method's. A third alternative would be to place
the object's VARIABLE declaration just before the implementation keyword
in the object's OWN unit...
******************************************
second example
******************************************
OBJECT ORIENTED 'DBHANDLER' IMPLEMENTATION
******************************************
OBJECT ORIENTED 'DBHANDLER' IMPLEMENTATION
******************************************
Complete (whole unit) sourcecode
Note the syntax for setting up both
table.dataBaseName [directory] [and]
table.tableName [filename]
unit StyleLog;
{Richard Ebbs for Honeywood Business Systems}
{this unit is the DATABASE FUNCTIONALITY unit of the 'SideStyle' project,
coded outside Fox to produce 'SIDE SECTION STYLE' BITMAP FILES that can
then be loaded into a Fox ListView component and thus give the user the
power to choose a style for a 'side section' (aka 'panel' in Cons2000).
Here we open a prexistent database table (that should reside in the
'BitMaps' directory under the exefile directory) and append a record
each time the user saves a bitmap, so building up a log of stylenames
against filenames in the table...}
interface
uses
DBTables {for TTable}, Forms {for TApplication},
SysUtils {for ExtractFileDir}, Windows, EditSty;
const
ssStylesDataBaseName = 'ssStyles.db';
bitmapFileDir = '\BitMaps\';
type
TDBHandler = class(TObject)
constructor Create;
destructor Destroy; override;
procedure AppendSideSectionStyleRecord(ssStyleName, ssFileName: String);
private
sssTable: TTable;
public
end;
var
DBHandler: TDBHandler;
implementation
constructor TDBHandler.Create;
{open the 'side section styles' database table...}
var
appName, appPath: String;
pathToDBTable: String;
tempString: String;
tableFileName: String;
begin
sssTable := TTable.Create(NewStyleForm);
{find the current path...}
appName := Application.ExeName;
appPath := ExtractFileDir(appName);
pathToDBTable := appPath + bitmapFileDir;
tableFileName := pathToDBTable + ssStylesDatabaseName;
sssTable.DatabaseName := pathToDBTable;
sssTable.TableName := tableFileName;
if FileExists(tableFileName) then
begin
sssTable.Open;
end
else
begin
tempString := 'The program cannot find the required database file (';
tempString := tempString + ssStylesDataBaseName;
tempString := tempString + '). Data cannot be saved to the database';
Application.MessageBox(PChar(tempString), ' Unknown File Location', mb_OK);
end;
end;
destructor TDBHandler.Destroy;
begin
sssTable.Free;
end;
procedure TDBHandler.AppendSideSectionStyleRecord(ssStyleName,
ssFileName: String);
{add a new record to the side section styles database table...}
begin
sssTable.Append;
sssTable.FieldByName('STYLENAME').AsString := ssStyleName;
sssTable.FieldByName('FILENAME').AsString := ssFileName;
sssTable.Post;
end;
end.