1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
Creating Custom Delphi Components, Part I
Page 5: Standards in component writing.
 More of this Feature
• Page 1: Why, when, how
• Page 2: New...component
• Page 3: Raw Code
• Page 4: Adding events

• Download Demo Projects

Printer friendly versionPrinter friendly version
 Join the Discussion
"Post your questions, concerns, views and comments to this article..."
Discuss!
 Related Resources
• Custom VCL development
• VCL with source
• Third party VCL
• VCL using

   Standards in component writing
Finally we will cover a few more points about component writing, including some existing methods of base components, and standards for writing.

Creating and destroying your component:
Objects are created through a constructor and destroyed through a destructor.
The purpose of overriding a constructor is threefold
1. To create any objects that it contains within itself (sub objects)
2. To initialise values of the class (properties etc)
3. To raise an exception and stop the class from being created

It is standard to call the inherited constructor from within your own constructor so that the parent-class can perform its initialisations, although it is not necessary to do so in order to create your component. (Your component is created as soon as your constructor is finished, it is not created by calling the inherited constructor)

The purpose of overriding a destructor is simply to free any resources that were allocated during the life of component. Call inherited only after you have freed these resources.

Standard component parts:
Paint: You can override this method to provide your own custom drawing of your component.

Loaded: This is called at runtime by Delphi as soon as all of its properties have finished being set when its parent form is created. You can override this method in order to perform any actions that depend on a group of properties all being set.

Invalidate: Whenever a property is changed that affects the visual appearance of a component you should call this method.

ComponentState: This property is very useful when you need to check if your component currently exists at design/run time, or if its properties are currently being read by a streaming process.

Encapsulating your component properly:
It is standard to write your component as TCustomMyClass and then derive your component from that base class. The "custom" component you write will have most (if not all) of its properties / methods declared within its Protected section. When you descend from your "custom" class you simply redeclare your properties within the Public or Published sections.

type
  TCustomMyClass = class(TComponent)
  private
    FSomeString  : String;
  protected
    procedure SetSomeString(const Value : String); 
      virtual;
    property SomeString  : String
      read FSomeString
      write SetSomeString;
  end;

  TMyClass = class(TCustomMyClass)
  published
    property SomeString;
  end;

This is good practise as it allows other people to derive their own components based on yours while still allowing them to remove certain properties.

Note how SetSomeString has been declared as virtual within the protected area. This is also good etiquette as it allows descended classes to respond to changes in property values by overriding the procedure that sets them. This also applies to events, where you see an OnStateChange event you will often find a DoStateChange method, for example:

type
  TCustomMyClass = class(TComponent)
  private
    FOnStateChange : TStateChangeEvent;
  protected
    procedure DoStateChange(State : TState); virtual;
  published
    property OnStateChange : TStateChangeEvent
      read FOnStateChange
      write FOnStateChange;
  end;

procedure TCustomMyClass.DoStateChange(State : TState);
begin
  if Assigned(OnStateChange) then 
    OnStateChange(Self, State);
end;

Instead of writing the "If assigned(OnStateChange) then" code every time the state changes, you would simply call "DoStateChange(NewState)". Apart from being smaller to write, it also allows descendent classes to override DoStateChange and trigger necessary code in response to an event.

   Summary
In this first article we have seen the uses of component writing. We have also seen how to write our components, and what original class to base our components on. Furthermore we discussed Virtual / Dynamic methods, and how to use them in order to implement "component etiquette". If you have any questions so far please post them on the Delphi Programming Forum.

In the second part of this article we will learn how to write custom properties, such as binary data, collections, sets, and expandable sub-properties.

First page > Why, when and how you should need to write a component. > Page 1, 2, 3, 4, 5

Creating Custom Delphi Components >>
>> Part II.

All graphics (if any) in this feature created by Peter Morris.

 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: Look Ma, I've found a BUG.
How do you know if a bug in your Delphi application is really yours? Windows applications are NOT bug-free; Delphi is no exception to this rule.
 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
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

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

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

All rights reserved.