Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
GENERAL BEST PRACTICES >>  SUB-CLASSING BASICS

  Ken Murphy
  Where is Ken Murphy?
 Springhill
 Canada
 Ken Murphy



1 - Why do I need sub-classes:

Take the following scenario: You are asked to make a change to all of the textboxes in your application. (For example, the user has vision problems and has asked you to change the disabled back color on all textboxes to something a little lighter than the grey that you get with the base classes.) If you have simply dropped the base class textbox on your form, you are going to have to find and change every single textbox in your app - it then gets worse. You also need to do the same thing to your edit boxes, list boxes, etc. A somewhat daunting task to say the least. Now, assume that you have used your own sub-classed text box, edit box, etc. All you need do is go into the class designer, change the disabled back color once for each class of control and recompile your app. A far less daunting task, wouldn't you say?

2 - How do I create my own sub-classes?

You can create your own sub-classes quite easily using the visual class designer. In your VFP project, go into the Class tab and click "New." VFP will pop a screen that asks for three things: What do you want to call this new sub-class? What do you want to base this sub-class on? And finally, where do you want to store this sub-class. To create your own sub-classes, begin by creating your own form sub-class. Call the new sub-class "__Form" (two underscores), select "Form" from the dropdown, and store it in "__Base.vcx" VFP will create a new __Form sub-class for you. Save and close your new class. Now go ahead and create a sub-class for each of the other base classes (editbox, textbox, etc.)

3 - How do I use these new sub-classes.

Go into the VFP menu and select Tools->Options. In the Options window, select the Forms tab, check the "Form" checkbox, click the elipsis and navigate to your new "__Form" class. Next, go into the "Controls" tab and click "Add". Navigate to your "__Base.VCX" and click OK. Click "Set as Default" and "OK."

Now, create a new form (VFP will now use your new __Form class) and in the VFP menu, View->Forms Controls Toolbar. Click the "View Classes" button in the toolbar and then select __Base. You can now drop your own __Base classes onto your forms.

4 - Can I retro-fit my existing forms with my new __Base classes?

Yes. One of the great things about VFP is that just about everything is stored in a table. Your forms are actually stored in tables too. You can therefore open the form as a table:

USE MyForm.SCX IN 0 EXCLUSIVE ALIAS MyForm
SELECT MyForm
BROWSE


Now, take a look at the "Class" and "ClassLoc" memo fields. If you wish to change your form's base class to your new __Form sub-class, you can issue a REPLACE command:

REPLACE ALL Class WITH [__Form], ;
   ClassLoc WITH [RelativePath2\__Base.VCX], ;
   FOR UPPER(Class] = [FORM] ;
   IN [MyForm]


You can issue a similar command for all of your other __Base controls. When you are done, you just need to recompile the form and you are done.

CAUTION: When "hacking" your production forms, care must be taken. Create a copy of the forms you are hacking and work on the copy. When you are satisfied that all went well, you can then copy the hacked form back into your production forms directory.

EDIT: Olaf pointed out to me that the FFC uses the single underscore as their naming convention. I have therefore edited this FAQ to use a double underscore in front of the class name. Of course, you can use your own naming convention as well. I once ran into a developer who actually used "my" as a prefix for his base classes - I don't necessarily recommend this particular naming convention though. :)

FEEDBACK

Olaf Doschke @ 6/13/2007 6:27:23 PM
Nice FAQ, Ken!

Beware, that _base.vcx is a classlib of foxpors FFC classes and if you want to use some ffc class, most of them are based on some class of _base.vcx.

I'd recommend to make another naming convention for the base classes. Craig Boyd has made some learning videos, in which he suggests that same step to subclass all basic classes. He blogged here:
http://www.sweetpotatosoftware.com/SPSBlog/PermaLink,guid,2baba34c-ebd6-46a3-aabe-35a4d7348014.aspx

Where you also find a download which contains - besides others - a classlib vfpclasses.vcx/vct, with those subclasses. Instead of _ he just uses a prefix c.

http://www.sweetpotatosoftware.com/SPSBlog/ct.ashx?id=2baba34c-ebd6-46a3-aabe-35a4d7348014&url=http%3a%2f%2fwww.sweetpotatosoftware.com%2flearningvfp%2f101%2fSource101.zip

You can save the time to do all this subclasses and simply use that vcx.

Bye, Olaf.

Ken Murphy @ 6/13/2007 7:55:58 PM
Thanks Olaf,

I will modify my FAQ to use a different naming convention.

Koen Piller @ 9/12/2007 10:39:32 PM
Ken,
a great tutorial. One more word on naming conventions, I have teached myself to avoid using _ as variable prefix. It is rather difficult to see in your normal VFP editor when your codeline, not yet complete, or (oh shame) with an error and therefore typically underscored. Agree with you a general prefix my is not so elegant, try to make it a habit to prefix with the convential letters so iNumber, cText e.s.o. instead of _iNumber. But afterall this is a solely personal flavor and in my opinion better than __ prefix.
Regards,
Koen
Please donot let this advise encourage you to stop posting your always interesting articles.

Ken Murphy @ 9/13/2007 8:49:49 PM
Koen,

I have to agree with you on the underscore. I am looking for a different prefix, but have yet to find anything I like better. I want a prefex that can, at a glance, show me that I am talking about a custom class definition. I don't like the "c" prefix because I use it for "Character." I thought of using "o" but again, I use that in object type variable names. I don't even like the underscore that much because I use it to identify #DEFINEd constants - but with constants, I normally use all upper case.

I thought about using "cl" but after trying it out for a few days, I found that my fingers want to type "lc" - I am used to using that for Local Character. Oh well - I guess I will continue to work with the underscore for the time being. (I don't want to change until I find something that will become permanent. I already have a bunch of code that uses the underscore - don't want to use two naming conventions in the same bunch of code.)

Thanks for your feedback.

Jim Wallace @ 3/6/2010 6:12:08 PM
Ken,

I know this is an old feed, but I thought I'd still drop a line with my sub-class naming convention.

I prefix the sub-class name with the original base class’ first letter.

For example.
fform
ttextbox
eeditbox
ggrid
llabel
ccommandbutton
etc…

If I need another sub-class based on the same base class or a sub-class of my sub-class I use a suffix with a lead underscore…

fform_standard
fform_nav
fform_listing
ffrom_preview
etc...

Regards to all



Your Name: 
Your Feedback: 

Spam Protection:
Enter the code shown: