12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

494 Day <strong>14</strong><br />

This declaration tells the compiler to call the GetColor() method of the class (the read<br />

method) when the Color property is read, and to call the SetColor() method of the class (the<br />

write method) when the Color property is written to. The exact implementation of the<br />

GetColor() and SetColor() methods is entirely up to the component writer.<br />

Properties can use direct access rather than read or write methods. A property almost always<br />

has a private class data member that is used to store the actual value of the property. It is<br />

common to use a write method to effect some change <strong>in</strong> the component when the property<br />

is written to, but no such method when the property is read. In that case you would specify<br />

a write method for the property but use direct access to read the property directly from the<br />

class data member that holds the property’s value. In that case the property’s declaration<br />

would look someth<strong>in</strong>g like this:<br />

__property <strong>in</strong>t Color { read=FColor, write=SetColor };<br />

Now when the property is written to, the SetColor() method is called as before, but when<br />

the property is read, the value of the FColor variable is returned rather than a read method<br />

be<strong>in</strong>g called. (The FColor variable would have been declared <strong>in</strong> the private section of the<br />

component’s class declaration.)<br />

A property need not declare either a read or write specifier. If no write specifier is <strong>in</strong>cluded,<br />

the property will be a read-only property (a valuable option at times). If no read specifier is<br />

<strong>in</strong>cluded, the property will be a write-only property (of dubious value). If neither a read nor<br />

a write specifier are <strong>in</strong>cluded, the property can neither be read nor written to, mak<strong>in</strong>g it a<br />

worthless property.<br />

Default Values<br />

Properties can specify the default value that the property should have when it is displayed <strong>in</strong><br />

the Object Inspector. For <strong>in</strong>stance, let’s say that your Color property should default to white<br />

(clWhite). In that case, the declaration of the property would look like this:<br />

__property <strong>in</strong>t Color { read=FColor, write=SetColor, default=clWhite };<br />

This tells the Form Designer to display the default value clWhite <strong>in</strong> the Object Inspector next<br />

to the property. Note that this does not automatically set the underly<strong>in</strong>g data member’s value<br />

to clWhite. You must still set the underly<strong>in</strong>g data member to the value clWhite <strong>in</strong> the<br />

component’s constructor. The use of the default specifier is optional. If you do not specify<br />

a default value, 0 will be used (or false <strong>in</strong> the case of bool properties) as the default value.<br />

Stor<strong>in</strong>g the Property<br />

The stored specifier controls how a property is stored <strong>in</strong> a form’s form file. Stor<strong>in</strong>g the<br />

property refers to stor<strong>in</strong>g the property’s value <strong>in</strong> the form file when the project is saved. The

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!