| RTL referenceGlossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To |
| Using Delphi objects to store configuration information | ||||||||||||||||||||||||||||||||||||
| The purpose of this article is to explain how to use objects to substitute the INI files (and other similar techniques) to store configuration information. | ||||||||||||||||||||||||||||||||||||
Article submitted by: Sebastián Mayorá
Introduction Some problems with ini files The solution is...
Delphi objects Take a look at this declaration
The main idea is create a class (which inherits from TComponent) with the properties and methods needed.
How long will our object live? To make sure that the object will always be available, we write at the end of the unit (before the end.)
This will allow us to use the object from any part of our application (even from the .dpr file! - See demo app-)
Properties The properties can be declared in the protected, public and published sections, only those declared inside published will be stored with the object.
Basic Declaration
(pressing Ctrl + Shift + C, Delphi generates a private variable FnoSplash: boolean)
When we write: the compiler translates it in: Declaring properties with restrictions (write)
(pressing Ctrl + Shift + C, Delphi generates a private variable fLines: TStrings and a private procedure SetLines with its body)
Note: Assigning fLines := Value would overwrite the internal pointer for FLines, lose memory, and create a number of ownership problems.
Note: This type of declarations of properties should ALWAYS be used to make a local copy of objects (any TObject descendant). For objects we usually will use Assign();
The Value parameter contains the new value that is about to be assigned to the property as a result of an assignment. We can check and manipulate the new value before completing the assignment. Also we can change other properties, call other procedures and functions, etc. (--See UserName property in the attached demo--)
When we write: the compiler translates it in: Declaring properties with masks (read)
(pressing Ctrl + Shift + C, Delphi generates a private variable fColor: Integer and a private function GetColor with their body)
When execution of the function terminates, whatever value was last assigned to Result or to the function name becomes the function's return value. We must always set a function's return value. If execution terminates without an assignment being made to Result or the function name, then the function's return value is undefined.
When we write: the compiler translates it in: Storing the properties Step 1
Next, we implement the procedure as follows
ReadRect and WriteRect are private or protected procedures of our class. For the DefineProperty method see Delphi help for additional information.
Step 2
(Pressing Ctrl + Shift + C Delphi generates the body for both procedures)
Step 3
... and read our property
NOTE: When it is possible, always use internal variables (FARect) instead of referencing properties (ARect) in a direct way.
Creating properties that are Objects Also we can initialize internal variables.
Implementing ...
What we create, we should destroy it:
Reading and storing our Object · WriteComponentResFile() and ReadComponentResFile() To store and to read the object we will use public procedures
Implementing...
If we want to use the Windows Registry we should make some small changes:
(ComponentToString and StringToComponent are implemented in the attached demo and they were taken from delphi help)
Observe the attached code for more information. (It contains explanatory comments)
|
||||||||||||||||||||||||||||||||||||
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:
More articles. Delphi programming articles by date... |
| Stay informed with all new and interesting things about Delphi (for free). |
|
|
| Got some code to share? Got a question? Need some help? |
