Sometimes you might find that a foundation class provides the basic functionality you want, but
you would prefer if it worked in a different way or provided additional functionality. This being
Visual FoxPro, and the FFC being classes that come with source code, you have almost unlimited
possibilities to subclass and extend them.
About Dialog Box
To use an example from earlier in this paper, suppose we like the FFC About Dialog Box but
would prefer that it always have the extra features such as the hyperlink label and the dynamic file
version label. You might be tempted to make changes directly to your copy of the _aboutbox
class in _dialogs.vcx, but of course the recommended way of doing anything like this in Visual
FoxPro is to create a subclass.
To do this, simply create a class library and add the _aboutbox class to it using names of your
choice. For example, to do this programmatically:
CREATE CLASSLIB myDialogs
CREATE CLASS myAboutBox OF myDialogs AS _aboutbox FROM HOME(1) + "ffc\_dialogs.vcx"
The new class is opened automatically in the class designer. Make your changes and save the
class.
Alternatively, if you've already made the desired changes to the About Dialog Box class and saved
it as a form, you can open the form in the Form Designer and choose Save As Class from the File
menu. Give the class a name and select a class library to store it in.
Either way, you now have your own subclass of the About Dialog Box. To use it, simply
instantiate your class object instead of the native FFC class object.
* Create an instance of your About Dialog Box.
loAboutBox = NEWOBJECT( "myAboutBox", "myDialogs")
loAboutBox.Show()
* Admire your work here...
loAboutBox = NULL
Before using this class in your apps, you would probably want to add code to set the captions of
the application name, copyright, and trademark labels from entries in the aVersion array, as has
already been done for the version number label. This is left as an exercise for the reader. (I've
always wanted to say that. <g>) For assistance, refer to the AGetFileVersion( ) Function topic in
the VFP Help File, which explains what's in each element of the array.
Working with INI Files
In a similar vein, I don't particularly like the order of the parameters in the OldINIReg foundation
class. I tend to think about INI files from top to bottom: first the file name, then the section name,
then the entry name, and finally the value. I'd like to be able the write the INI class method
parameters in that order. This can be easily accomplished by creating a subclass of OldINIReg.
Start by creating a subclass of OldINIReg in your own class library.
CREATE CLASSLIB myRegistry
CREATE CLASS myINIClass OF myRegistry AS oldINIReg FROM HOME(1) + "FFC\Registry.vcx"
Modify the GetIniEntry( ) method to accept the parameters in the desired order, then pass them to
the default code in the expected order. Note that you need to pass the cValue parameter by
reference, since it will contain the return value.
*!* LPARAMETER cValue,cSection,cEntry,cINIFile && old order
LPARAMETER cINIFile, cSection, cEntry, cValue && new order
* Pass the parameters to the default code in the expected order.
* Be sure to pass cValue by reference as it will contain the return value.
DODEFAULT( @cValue, cSection, cEntry, cINIFile)
Make the same changes to the WriteIniEntry( ) method, except you do not need to pass the
cValue parameter by value.
The session download include the class library myRegistry.vcx, which contains the class
myINIClass with the modifications shown above. The downloads also include myINIDemo.prg, a
program to drive the new class.
These are but two examples of how you can subclass and extend the FoxPro Foundation Class It's really true that you are limited only by your imagination in how far you can go with this.
No comments:
Post a Comment