| 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. | ||||||||||||||||||||||
Starting to write our component 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.
Test drive 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'.
Clicking the "Start" button will mark the start time (GetTickCount is a WinAPI command that returns the number of milliseconds since Windows started). Virtual, Dynamic, Abstract and Override 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.
We then implement the above code as follows.
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 >> |
||||||||||||||||||||||
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). |
|
|
| Got some code to share? Got a question? Need some help? |

