Mega Code Archive

 
Categories / Delphi / Examples
 

Coding Tip..(pay it forward)

Title: Coding Tip..(pay it forward) Question: How do handle dynamic object creation in a neat and effective way ? Answer: When one creates objects dynamically, as needed, your code inevitably becomes quite messy. Checking to see if the object exists before creating it, eg. if FMyVariable = nil then FMyVariable := TMyClass.Create; Checking to see if the object exists before destroying it, e.g if FMyVariable nil then FreeAndNil(FMyVariable) Checking to see if the object exists before using it, e.g if FMyVariable = nil then begin FMyVariable := TMyClass.Create; FMyVariable.SomeMethod; end; OR if FMyVariable nil then FMyVariable.SomeMethod; Inevitably your code becomes scattered with code like the fragments shown above making maintenance a real difficult task. If you needed to change the create you would have to hunt all over looking for the "if nil then create" or "if = nil then create", with 4 simple methods you can neaten up your code and give maintenance programmers a break. The four simple methods are : procedure CreateObject; procedure FreeObject; function ObjectAllocated: Boolean; procedure ObjectNeeded; and this is how the code for each method looks; procedure CreateObject; begin FObject := TObject.Create; end; procedure FreeObject; begin if ObjectAllocated then FreeAndNil(FObject); end; function ObjectAllocated: Boolean; begin Result := (FObject nil); end; procedure ObjectNeeded; begin if not ObjectAllocated then CreateObject; end; And thats it, so instead of using : "If FObject nil then" you use "if ObjectAllocated then", when you need the object simply call ObjectNeeded, and to destroy the object simply call FreeObject. All the nitty-gritty if existance checking is already taken care of. Here is a simple demo program that uses this technique: DEMO APP: Main Form in Text:: object Form1: TForm1 Left = 371 Top = 213 Width = 236 Height = 146 Caption = 'Demo' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnDestroy = FormDestroy PixelsPerInch = 96 TextHeight = 13 object Button1: TButton Left = 8 Top = 8 Width = 100 Height = 25 Caption = 'Create Object' TabOrder = 0 OnClick = Button1Click end object Button2: TButton Left = 120 Top = 8 Width = 100 Height = 25 Caption = 'Free Object' TabOrder = 1 OnClick = Button2Click end object Button3: TButton Left = 8 Top = 44 Width = 100 Height = 25 Caption = 'Show Classname' TabOrder = 2 OnClick = Button3Click end object Button4: TButton Left = 120 Top = 44 Width = 100 Height = 25 Caption = 'Check Existance' TabOrder = 3 OnClick = Button4Click end object Button5: TButton Left = 76 Top = 84 Width = 75 Height = 25 Caption = '&Close' Default = True TabOrder = 4 OnClick = Button5Click end end Main form unit :: unit FMain; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; procedure Button5Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure FormDestroy(Sender: TObject); private FObject: TObject; procedure CreateObject; procedure FreeObject; function ObjectAllocated: Boolean; procedure ObjectNeeded; public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} const ExistIdents: array[Boolean] of string = ('Object does not exist', 'Object Exists'); { TForm1 } procedure TForm1.CreateObject; begin FObject := TObject.Create; end; procedure TForm1.FreeObject; begin if ObjectAllocated then FreeAndNil(FObject); end; function TForm1.ObjectAllocated: Boolean; begin Result := (FObject nil); end; procedure TForm1.ObjectNeeded; begin if not ObjectAllocated then CreateObject; end; procedure TForm1.Button5Click(Sender: TObject); begin Close; end; procedure TForm1.Button1Click(Sender: TObject); begin ObjectNeeded; end; procedure TForm1.Button2Click(Sender: TObject); begin FreeObject; end; procedure TForm1.Button3Click(Sender: TObject); begin ObjectNeeded; ShowMessage(FObject.ClassName); end; procedure TForm1.Button4Click(Sender: TObject); begin // if ObjectAllocated then // Showmessage('Object does not exist') // else // Showmessage('Object Exists'); // this does exactly the same as above, but is also a preferred technique ShowMessage(ExistIdents[ObjectAllocated]); end; procedure TForm1.FormDestroy(Sender: TObject); begin FreeObject; end; end. Good luck, Derrick....(dnel@oldmutual.com) "programming in VB is like kicking a dead whale down a beach !!".