| RTL referenceGlossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To |
| Creating Custom Delphi Components - Inside and Out | |||||||||||||||||||
| Page 1: About components, properties, methods and events | |||||||||||||||||||
Article submitted by: Alistair Keys
What's This Tutorial About? What Are Components? Have a look at the Delphi component palette. You will see all sorts of
objects - buttons, checkboxes, shapes and so on. If you find you have a
recurring task that is getting pretty monotonous, you can create a component to
do the job for you. Once you've finished it, you can install it and use it
like any other. Properties When you are defining properties, you usually specify three things:
its data type, how to read the property and how to write the
property (if applicable). The first is obvious; if you want the
property, you need to
know what it is actually representing! The next two aren't quite as obvious. Accessing properties seems straightforward... we've all done it (e.g. "Button1.Caption :=
'xyz';", where we're accessing Button1's Caption
property). This just seems like a simple variable assignment, and that's
probably what you assumed it was. Well, it's not! Delphi performs some
trickery and actually does some work behind the scenes. So: When accessing properties, you could either be using variables directly or calling functions behind the scenes! The standard property declaration looks like this:
This defines a property "SomeProperty", of type Integer. Note
that properties are not actually variables in themselves, just ways of
accessing other variables! Thus, the variable actually accessed with "SomeObject.SomeProperty"
will be FMyInteger. Now for the interesting bit: the read and write parts
say how to access the data. In this case, when the property is read (e.g.
"if SomeObject.SomeProperty = 1") the value is taken directly
from the variable, as if the code had said "SomeObject.FMyInteger".
However, when the property is set, it is not done
directly! The code actually calls a SetMyInteger procedure, which would be
of the form:
This gets called instead, as if the code had said
"SomeObject.SetMyInteger(23)" instead of "SomeObject.SomeProperty
:= 23". Now, the read and write parts can specify direct data access (by putting the
variable name) or via procedures/functions (by putting the procedure/function
name). To give another example, here's a read-only property that gets its
data from a function:
This property cannot be written to, which safeguards the
data nicely :-). The GetMyValue function would be of the form: "
Advantages of Properties The first one is pretty obvious, really. If you declared your variables
publicly they could be changed in any old way. However, you can
still allow direct access to your variables if you so desire. Properties
allow you to change the behind-the-scenes way of getting or setting data without
having to rewrite any code that uses the component! The second point is something that makes you really appreciate Delphi.
In other languages (for example C++) you do need to say
"someObject.setThisValue(23)" everywhere. This makes your code
less readable and just plain sucks! It, in my view, reduces the
readability as it detracts from your main intent: you want to change the value,
not call the function! Delphi has provided an elegant mechanism to avoid this
kind of unnecessary code. The last point is good for efficiency - if you have a graphical component, for
example, you want to reduce the drawing as much as possible. This can be
done by a set procedure that checks if the old value is different from the new
value. Here's an example:
This means that your component will only redraw itself if it gets a different
property value. This technique can be applied elsewhere, with error checks
and other such data checking.
Methods Events Question, Suggestions... Next page > New...Component >
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). |
|
|
| Got some code to share? Got a question? Need some help? |

