1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL referenceGlossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
Creating Custom Delphi Components - Inside and Out
Page 6: On Destructors and Methods
 More of this Feature
• Page 1: About components
• Page 2: New...Component
• Page 3: On Fields
• Page 4: On Properties
• Page 5: On Constructors
• Page 7: On Events
• Page 8: Hiding data
• Page 9: Virtual functions and Abstract Classes
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• VCL Writing/Enhancing
• Custom Component Dev.
• OOP in Delphi
• Free Source Components

   Destructors
Destructors are the logical opposite of constructors. They are responsible for squishing your object as appropriate: if you allocated memory, you need somewhere to deallocate it; if you opened a file, you need somewhere to close it. This type of job can be done in a destructor.

As with the standard Create constructor, every object gets a default destructor from TObject, called "Destroy". Call this when you're finished with an object to do any cleaning up necessary, like this:

 SomeObject.Destroy;

You can also use "SomeObject.Free", which is a wrapper for the Destroy destructor but with a check: "if Self <> nil then Destroy;" This is safer, so I recommend you use "Free" instead. I'll discuss 'Self' later. So how do you put destructors into your classes? Well, here's a simple example:

type
  TSomething = class(TObject)
  public
    destructor Destroy; override;
  end;

implementation

destructor TSomething.Destroy;
begin
  // do any cleaning up necessary here
  inherited;
end;

This code should give you a warm sense of familiarity, as it's almost the same as the constructor code. There's only one difference - "override". This is because the standard Destroy destructor is declared as Virtual. I'll talk about Virtual methods later on in this tutorial, so don't worry - it's just to do with inheritance and (scary big word alert!) "polymorphism".

Well, most of the same points for constructors apply to destructors. Again, you can have many destructors with different names, and again it's probably not a wise move. There's not really much to go over, then, is there? Let's go on to the next topic...

   Methods
I've already introduced methods above. Simply put, they're any procedure, function, constructor or destructor that belongs to an object (a class type). There's nothing more complicated to them than that, so don't be afraid of another jargon word. To use methods, you declare the method in the class's prototype (in the "type" section at the top of the unit) and write the actual code in the implementation. An example:

type
  TGeneric = class(TObject)
  public
    procedure ThisIsAMethod(const something: String);
  end;

implementation

procedure TGeneric.ThisIsAMethod(const something: String);
begin
  // use "something" as appropriate 
end;

That code should not scare you in the slightest. You've been doing similar things with your forms. If you look in your form's code, you might spot the bit "TSomeForm = class(TForm)". Hopefully you now understand what's going on - each time you use a form, it gets inherited from TForm, and you customise it as appropriate, which is the standard inheritance concept. When you write methods for you classes, the only thing to remember is that they have the class name followed by a period (dot) before the name - like "TGeneric.Whatever". Other than that, they're completely straightforward. The only thing you need to be aware of, "Self", will be talked about in the next section, namely...

   Question, Suggestions...
If you have any questions or comments to this (huge) article, please post them on the Delphi Programming Forum. Discuss!

Next page > On Events > Page 1, 2, 3, 4, 5, 6, 7, 8, 9

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: GDI Graphics In Delphi.
From simple lines to direct API calls: the ultimate tutorial to GDI graphics in Delphi. This tutorial will introduce you to some of the techniques in the GDI Delphi drawing. Look for: drawing lines and shapes, drawing pictures, flicker-free drawings, off-screen bitmaps, GDI drawings the API way...
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?

Explore Delphi Programming

More from About.com

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2008 About.com, a part of The New York Times Company.

All rights reserved.