Article ID:qFRM001
Date Revised:July 23, 1997
Keywords:Form, Multiple return values, OOP
Synopsis:Use an interface object
See Also:Click here to download this example formtalk.zip 

Question: How can I get a form to return more than one value?

Answer: One way of accomplishing this is to use an interface object, that has a property for each of the values that you want to return. You can create the object, set it's property values and then pass this object to the form. The form can then put the return values into the object properties as the form is closing.

The object could be defined like this:

**************************************************
*-- Class:        frmgetnameinterface (h:\vfp5app\df_foxpro\formtalk.vcx)
*-- ParentClass:  cobject (h:\vfp5app\df_foxpro\formtalk.vcx)
*-- BaseClass:    line
*
DEFINE CLASS frmgetnameinterface AS cobject

   *-- The first name
   mcfirstname = ""
   *-- The last name
   mclastname = ""
   *-- Their birthday
   mdbirth = {  /  /  }
   *-- The City
   mccity = "Lynchburg"
   Name = "frmgetnameinterface"


   *-- Show the property values
   PROCEDURE display
      ? this.mcFirstName, this.mcLastName, "was born in", this.mcCity, "on", this.mdBirth
   ENDPROC


   PROCEDURE Init
      lparameters lcFirstName, lcLastName, lcCity, ldBirth

      * allow these properties to be set at Init() time

      if ( ( ! empty( m.lcFirstName ) ) and ( type( "m.lcFirstName" ) == "C" ) )
         this.mcFirstName = m.lcFirstName
      endif

      if ( ( ! empty( m.lcLastName ) ) and ( type( "m.lcLastName" ) == "C" ) )
         this.mcLastName = m.lcLastName
      endif

      if ( ( ! empty( m.lcCity ) ) and ( type( "m.lcCity" ) == "C" ) )
 this.mcCity = m.lcCity
      endif

      if ( ( ! empty( m.ldBirth ) ) and ( type( "m.ldBirth" ) == "D" ) )
         this.mdBirth = m.ldBirth
      endif
   ENDPROC

ENDDEFINE
*
*-- EndDefine: frmgetnameinterface
**************************************************

The form Init() method:

lparameter roInterface

if ( ( pcount() = 0 ) or ;
     ( type( "m.roInterface" ) != "O" ) or ;
     ( isnull( m.roInterface ) ) )
   MessageBox( "I need an object to work on" )
   return .f.
endif

this.moInterface = m.roInterface && keep reference so we can use it later

with this.moInterface
   this.txtFirstName.Value = .mcFirstName
   this.txtLastName.Value  = .mcLastName
   this.txtCity.Value      = .mcCity
   this.txtBirthdate.Value = .mdBirth
endwith

In the OK CommandButton Click() the form values are put back into the object:

* Save the edit results to theinterface object properties

with thisform.moInterface
   .mcFirstName = thisform.txtFirstName.Value
   .mcLastName  = thisform.txtLastName.Value
   .mcCity      = thisform.txtCity.Value
   .mdBirth     = thisform.txtBirthdate.Value
endwith

thisform.Release()

To use the interface object and the form the code would look like:

* FormTalk.PRG 23-Jul-97

* Demo how to use a form interface object to return multiple values from a form

set classlib to formtalk additive

* create the interface object, setting some fo the properties

oPerson1 = createobject( "frmGetNameInterface", "Dave", "Frankenbach", "DeSoto", {10/04/57} )

* call the modal form

do form getname with oPerson1

* show the result of the edits

oPerson1.Display()


* If you'd rather set object properties than remember parameter order

oPerson2 = createobject( "frmGetNameInterface" )
oPerson2.mcFirstName = "Katie"
oPerson2.mdBirth     = {03/20/91}

do form GetName with oPerson2

oPerson2.Display()


1