Objects as parameters in Visual FoxPro

A procedure, function or form in Visual FoxPro cannot return more than a single parameter and sometimes this is not enough. The traditional solution in earlier versions of the language was to create an array to hold the multiple values and to return that array as the single parameter. The introduction of object orientation in Visual FoxPro gives us a more flexible alternative.

As an example, consider a form which asks the user to enter two dates, the start and end of a period. This form must return three parameters. Two of these are obvious, we need the start date and the end date. The third parameter is more subtle and is often overlooked; we can rely on the form to validate the dates and ensure that the end is later than the start but the code that calls the form must know whether the user has pressed the 'OK' or the 'Cancel' button. The form has a boolean property lSelected which is .F. by default and is set .T. when the user presses the 'OK' button.

[DateRange form in Visual FoxPro]

The form can return these three values as properties of an object so we need a simple class as the basis of this object:

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.

When a form closes the Unload event is the last to run and it is this that returns the parameter. By this time, every object on the form has been destroyed and we cannot read the date in either of the textboxes. Any value returned by a form has to be retrieved from a property of the form because nothing else is 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')

We must store the dates from the textboxes in this object before the form closes. If the user presses the 'OK' button then we store the start and end dates and .T. in the three properties. The click event holds the following code:

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

The code for the 'Cancel' button is much simpler:

With ThisForm
   .oDateRange.lSelected = .F.
   .Release
EndWith

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

Return ThisForm.oDateRange

Access Tips

FoxPro Tips

General Tips

 

Related Items

Alvechurch Data - Microsoft FoxPro and Access Database Development and Training

Alvechurch Data provide training and development support for businesses using Microsoft FoxPro, Visual FoxPro and Access databases.

Read More

Autocomplete in Visual Fox Pro

Autocomplete in Visual Fox Pro

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.

Read More

FoxPro Functions

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

Read More

Development Services

Alvechurch Data have fifteen years experience developing databases for small business use.

Read More

Training Services

Details of the training courses offered by Alvechurch Data.

Read More