Object parameters in Visual FoxPro


A function or form in FoxPro can return a single parameter. Sometimes this is not enough and the traditional response was to store the multiple parameters in an array. Object orientation in Visual FoxPro allows a more flexible approach.

As an example, consider a form which asks the user for a pair of start and end dates. This form must return three parameters. Two are obvious, the start and end dates. The third is more subtle but equally important; we need to know whether the user has entered dates or pressed Cancel. There's a boolean property lSelected for this.

[DateRange form]

First we need a simple class with properties to hold these three values:

DEFINE CLASS xdaterange AS custom

   Name = 'xdaterange'

   *-- Start date of period
   dfromdate = .F.

   *-- End date of period
   dtodate = .F.

   *-- .T. if dates have been selected
   lselected = .F.

ENDDEFINE

Next, the form needs a property to hold this object. Any value returned by a form has to be retrieved from a property of the form because none of the controls on the form are available at the moment when the form closes and returns its parameter. Code in the form's Init method must put a DateRange object into this form property.

ThisForm.oDateRange = CREATEOBJECT ('xDateRange')

Next, we must put the values from the form into this object. If the user presses the Save button then we store the two dates and .T. in the three properties.

With ThisForm
   .oDateRange.dFromDate = .txtFromDate.Value
   .oDateRange.dToDate = .txtToDate.Value
   .oDateRange.lSelected = .T.
EndWith

If the user presses Cancel then we store empty dates and .F.

Finally, we put a Return statement in the Unload event of the form.

RETURN ThisForm.oDateRange

Hints & tips

The textbox class in Visual FoxPro 9 has a new Autocomplete property which shows the user the previous values that have been entered in that textbox.
Autocomplete in VFP 9

Your Access database will look more impressive if you add custom toolbars...
Custom toolbars

FoxPro has always had functions to read and write files at a low level...
Foxpro low level file functions

More...
More pages of hints and tips for users of Microsoft FoxPro and Access databases.

Site Map