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 3: Component raw code; Virtual, Dynamic, Abstract and Override keywords.
 More of this Feature
• Page 1: Why, when, how
• Page 2: New...component
• Page 4: Adding events
• Page 5: Writing standards

• Download Demo Projects
 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

   Starting to write our component
Delphi now needs to know all about our new component. Enter the following code into the component source code.

  private
    { Private declarations }
    FStartTime,
    FStopTime  :DWord;
  protected
    { Protected declarations }
    function  GetElapsedTime :
	String; virtual;
  public
    { Public declarations }
    procedure Start; virtual;
    procedure Stop; virtual;

    property StartTime:DWord
      read FStartTime;
    property StopTime:DWord
      read FStopTime;
    property ElapsedTime:String
      read GetElapsedTime;
  published
    { Published declarations }
  end;

What we have done here is added two variables FStartTime and FStopTime (it is standard to preceed variable names with the letter F). There are two methods for controlling these variables, Start and Stop. We have added a GetElapsedTime function which will return FStopTime - FStartTime as a string. Finally we have added three read-only properties.

Press SHIFT-CTRL-C and Delphi will automatically complete the code for your class (or click the right mouse button and select "Complete class at cursor"). Next enter the following code for each respective method.

{ TFirstComponent }

function TFirstComponent.GetElapsedTime: String;
begin
  Result := IntToStr(FStopTime - FStartTime);
end;

procedure TFirstComponent.Start;
begin
  FStartTime := GetTickCount;
end;

procedure TFirstComponent.Stop;
begin
  FStopTime := GetTickCount;
end;

end.

   Test drive
Save your unit, and reopen your package (File, Open Project from the menu, and select "Delphi Package" for the file type), once your package is open click the "Compile" button. You can also open your package by choosing Component from the main menu and then Install Packages. Select your package and then click the "Edit" button.

You can now drop a TFirstComponent onto a form, in fact, you can drop as many as you like. Add two buttons (btnStart and btnStop) and add the following code to your form, and then run your test app'.

procedure TForm1.btnStartClick(Sender: TObject);
begin
  FirstComponent1.Start;
end;

procedure TForm1.btnStopClick(Sender: TObject);
begin
  FirstComponent1.Stop;
  Caption := FirstComponent1.ElapsedTime;
end;

Clicking the "Start" button will mark the start time (GetTickCount is a WinAPI command that returns the number of milliseconds since Windows started).
Clicking the "Stop" button will mark the stop time, and change the caption of the form.

   Virtual, Dynamic, Abstract and Override
You may have noticed the Virtual declaration after Start, Stop and GetElapsedTime. The following exercise will explain their uses.

Create a new component, derive this component from TFirstComponent (name it TSecondComponent) and install it.

The Virtual and Dynamic identifiers are a component writer's way of telling Delphi that the method may be replaced in a descendent class. If we Override a method in a class, our new code will be executed instead of the original code.

protected
  { Protected declarations }
  function  GetElapsedTime : String; override;

We then implement the above code as follows.

function TSecondComponent.GetElapsedTime: String;
var
  S : String;
begin
  S := inherited GetElapsedTime;
  Result := S + ' milliseconds or ' + 
    Format('%.2f seconds',
      [StopTime - StartTime) / 1000]);
end;

Our new code is now called in replacement of the original GetElapsedTime, even calls in TFirstComponent to GetElapsed time will now call our new code. The original code is invoked through the use of the Inherited command.

Note : If you do not "override" a base method (because the base was not declared as Virtual or because you forgot). TSecondComponent will call your new code, whereas and code introduced in TFirstComponent will still continue to call the original code from TFirstComponent.

(See Demo2)

The Abstract identifier tells Delphi not to expect any code for the named method. You should not create an instance of any object with abstract methods in them (such as TStrings). The standard practise is to create a descendant of such a class and to override all abstract methods (such as TStringList does).

Dynamic Vs Virtual is simply a question of speed Vs size. A Dynamic method will result in each instance of a class requiring less memory, whereas a Virtual method will execute faster at the cost of a little extra memory.

Next page > Adding events > 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.