HOME | SIGN UP | ACCOUNT | SUBSCRIBE | STORE | WHAT'S NEW | TESTIMONIALS | TROUBLESHOOTING | SUPPORT AREA | SITE NEWS | LOGIN
Classes & methods · News · Jobs · Links · Downloads · Articles · Conferences · Magazines · Training · Consultants · Consulting · Blogs · FAQ
Books · Applications · Meetings · User groups · Speakers · User group leaders · Universal Thread Magazine · Coverages · Photos · Videos

 ID: 28616Created on: October 7, 2005 23:36Updated on: April 22, 2010 21:35

Inheritance tutorial
Summary:This is a step-by-step tutorial to show inheritance, specifically in Visual FoxPro forms, as a guidance for people who are not familiar with inheritance in general, or who don’t know how to implement it in Visual FoxPro. The basic idea of inheritance is that all your forms, or several of your forms, obtain their properties and methods from a "base form". It is possible to do changes only once, in your "base form"; the changes will be propagated to all your forms.
Rating: Rate this entry
Created by:
Hilmar Zonneveld
Independent Consultant
Cochabamba

Introduction

The following should work in all versions of Visual FoxPro (i.e., version 3 and later).
The examples are not useful by themselves, they only serve to show how to use inheritance.


Create a new project

modify project inheritance


Create the base class
Select “Classes”, click on “New”. Name your class cMyForm, and base it on the class “Form”. Store it in class library MyClasses.

Give your form some color, selecting the property “BackColor” in the Property Sheet.
Save it.


Create a derived form

Create a form that is based on the class, with the command:

create form MyForm as cMyForm from MyClasses


Note that the form has the same BackColor as the one you defined in the class.
Save your form.


Propagating changes

Go back to the class (open class library MyClasses in the Project Manager, click on the “+” sign, and open class cMyForm).

Change the background color (change property BackColor in the Property Sheet).
Save the class, and open the form:

modify form MyForm


Note that the form’s background color changed.

Breaking inheritance


Change the BackColor directly in the form. If you do this, inheritance is “broken”, that is, any change in the BackColor of the class will no longer be applied in the form.

To restore inheritance, right-click on the BackColor in the Property Sheet, and select “Reset to Default”.


Inheriting behavior

Until now, we have only seen how to inherit properties.

Now, go to the base class (cMyForm), add a button, and put the following command in the Button’s Click() Event:

MessageBox("Hello")


Run your form:

do form MyForm


then click on the button, and confirm that the message appears on screen.


Adding to the inherited behavior

If you write anything in the Click() Event of the button on the form, inheritance is broken – the code from the parent class is not executed.
For example, write the following in the Click() Event of the form:

MessageBox("This is the second command.")


The original code is no longer executed.

Usually you will want to include the original commands, and just add something in a derived form. Change the button’s Click() Event to:

DoDefault()
MessageBox("This is the second command.")


Run the form again, and verify that both MessageBoxes show, first one, then the other.


Additional considerations

  • The practical value of all this is that any change done in the class is propagated to all forms that derive from the class. Of course, you can base several forms on the same form class.
  • A class can derive from another class; you can have a hierarchy of inheritance.
  • You can also use inheritance for Buttons, TextBoxes, etc. For example, you could define a common behavior for all TextBoxes that refer to money amounts.
  • In older versions of Visual FoxPro, a form class can not use a DataEnvironment. You have to open your tables with commands, in the Form’s Load() Event. Common commands include USE, SET ORDER, CursorSetProp() (for buffering), SELECT, SET RELATION.
  • For information about common problems, see also my other FAQ, on breaking inheritance – but first practice a while to get familiar with inheritance in general.

    Comments
    Add a comment

    More entries from this member

    1. Breaking Inheritance December 6, 2001
      (The latest update contains minor edits only.) Five easy and fun ways to get yourself into trouble with inheritance. A frequent source of problems in OOP is called "breaking inheritance". This document briefly describes what inheritance is, how it applies to properties and methods, and how it can be maintained or broken. Problems appear especially when inheritance is broken in methods.

    2. CHM help file shows error messages instead of help pages October 6, 2005
      Due to a recent Windows security fix, users can no longer access a CHM file on a server. The table of contents appears, but the individual pages are replaced by error messages. Access to CHM files in specific folders can be explicitly allowed through special registry settings.

    3. Control Arrays July 20, 2001
      (The last update contains minor edits only.) The idea is to have several controls on a form controlled with an array. Thus, you can quickly go through all the controls on the form, managing the array. The sample code included will help you get started quickly. You can easily adapt it to manage arrays of any type of control on your forms (or other containers).

    4. Least common multiple and greatest common factor May 30, 2004
      The code shows how to quickly obtain the greatest common factor, and the least common multiple. Both functions are used when manipulating fractions, among others. Several methods are possible; the method usually taught in school involves prime numbers, but this code will execute much faster (and it is much simpler) than the use of prime numbers.

    5. Open any document November 8, 2001
      The following function will open any document, with its default association (the same application that will be called when you double-click on the file, in Windows Explorer). Use it to open a text-file, a Word or Excel document, an image, etc., with an external application.

    6. Rushmore Optimization - not always optimal July 20, 2001
      Rushmore Optimization can help make queries much faster. However, "Full Rushmore Optimization" is not always a desirable goal. "Partial Optimization" is sometimes much faster. It is often believed that to speed things up, you need to have as many indices as possible. This article explains that some indices may actually make queries slower. Also, some additional tips on query optimization.

    7. Show seconds in a readable format June 7, 2002
      If you need to check elapsed time with seconds() or a datetime value, this function allows you to display the elapsed time in a human-readable format, that is, hours:minutes:seconds, instead of the total number of seconds. Just pass a number of seconds as a parameter.

  • TechEd 2003
    DateContentDetail
    06/09 16:31MeetingSQL Server Tools, Strate...
    02/09 00:44NewsPEM Editor 6 has been re...
    27/08 17:57NewsSouthwest Fox 2010 Early...
    25/08 04:30NewsCryptoLicensing For .Net...
    22/08 09:58NewsWindows Phone 7 Gold Rush
    16/08 03:05NewsVisual WebGui reveals it...
    11/08 17:54MeetingSo You Think You Want To...
    10/08 16:41News.NET Training for VFP Pr...
    09/08 18:33MeetingLINQ & SQL Server Reporting
    03/08 08:15JobsSQL/VFP Developer for Ma...
    29/07 10:59News.NET 4 for VFP Developer...
    26/07 10:13JobsVFP Developer (Two Perma...
    25/07 06:44NewsWith Visual WebGui 6.4 R...
    24/07 11:00JobsQuality Assurance Softwa...
    24/07 10:53JobsIntegrations and Web Dev...

    Copyright © 1993 - 2010 Level Extreme Inc., All Rights Reserved · Telephone: 506-783-9007 Email: info@universalthread.com · Privacy & Security · Copyright · Terms & Conditions