Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning multiple values from a form

Status
Not open for further replies.

steinkebunch

Programmer
May 24, 2000
7
0
0
US
Can a DO FORM command be used to return the value of more than 1 variable to a program? I have the following code in my program, but when I compile it, it gives me a syntax error. If I remove "retval2", it works fine.

Code:
DO MyForm2 WITH var1, var2 TO retval1, retval2

Thanks for your help

 
Hi,
Consider public variables. Or you initiate 2 variables before DO FORM and that form will changes these varibles. They will still exist after that form release.

Jimmy mailto:nhan_tiags@yahoo.com
 
You can also consider using form classes and use the createobject function.
You will have to add a parameter statement to the init of your class.

Weedz
veld4663@exact.nl

'It never hurts to help...' - Eek the cat.
 
Can a DO FORM command be used to return the value of more than 1 variable to a program?

The simple answer is: No.
From the MSDN (see DO FORM):
(..)
TO VarName

Specifies a variable to hold a value returned from the form. If the variable doesn't exist, Visual FoxPro automatically creates it. Use the RETURN command in the Unload event procedure of the form to specify the return value. If you do not include a return value, the default value of true (.T.) is returned. If you use TO, the WindowType property of the form must be set to 1 (Modal).
(..)

A simple solution is the usage of publics/privates to set more than 1 return value. Eg. create a variable, start the form; add code to the form to change the variable and there you are.

A "neater" solution (and a bit more work) is the usage of form-classes. In that case, it's possible to create methods with which you can obtain whatever value you need (or even simpeler, by just using properties...)
Say, we have a form class called "myform". The INIT() method has 2 parameters. We've created a (public) method QueryResult() which returns something, and a (public) property cReturnText. Our form does something so that the method and the property return something.
Now, all we need to do is:
Code:
oForm = CREATEOBJECT(&quot;MYFORM&quot; <parameterlist>)
oForm.Show()
result = oForm.QueryResult()
text   = oForm.cReturnText()
oForm.Release()

Do note that this works only if the form doesn't releases itself!
Diederik Vermeeren
verm1864@exact.nl
 
Hi steinkebunch,

As the others have mentioned, the answer is No, you cant.

One of the most common work-around implementations is Diedrik's &quot;neater&quot; solution, where the form HIDEs itself instead of RELEASEing. Then the caller function extracts the necessary info from the form and then RELEASEs it.

Another solution is to use what VFPers have come to refer to as &quot;Parameter objects&quot;.
For a more detailed explanation of a parameter object, see the following article by Doug Hennig, &quot;The OOP-Guru&quot;:
Manage Your Applications

A simplified example of how to return a username &amp; password from a login form using a parameter object:

DO FORM MyLoginForm TO oLogin
? oLogin.icUserName
? oLogin.icPassword
RELEASE oLogin

PROCEDURE MyLoginForm.Unload
oRetObject=CREATEOBJECT('CUSTOM')
oRetObject.ADDPROPERTY('icUserName',THIS.icUserName)
oRetObject.ADDPROPERTY('icPassword',THIS.icPassword)
RETURN oRetObject
ENDPROC Jon Hawkins
jonscott8@yahoo.com

The World Is Headed For Mutiny,
When All We Want Is Unity. - Creed
 
Hello.

Or you can try to use an array as parameter.
The called form can fill the array, and then return the array.

Personally, I prefer this method.

Hope this helps.

Grigore Dolghin
 
Just to clarify a few points for future on-lookers:

then return the array

You can NOT return an array in VFP.
For Instance,

PROCEDURE MyTest
DIMENSION MyArray[5]
*populate the array elements
RETURN MyArray
ENDPROC

will only return the first element of MyArray.

use an array as parameter.
The called form can fill the array


You can pass an array to a method of the form by reference and the form can manipulate the array. The problem is, you can NOT store a reference to the array beyond the method it was passed to.

For Instance,

DIMENSION laMyArray[3]
DO FORM MyForm with laMyArray

PROCEDURE MyForm.Init
LPARAMETERS taMyArray

taMyArray[2]='CHANGE'
ENDPROC

After the Init event, the By Ref array falls out of scope. You can't store a variable reference like you can an object reference from the called method. Bummer, but that's life. Jon Hawkins
jonscott8@yahoo.com

The World Is Headed For Mutiny,
When All We Want Is Unity. - Creed
 
Hello.

I'm sorry, but I have to say one more thing:

I check thoroughly every word I write in a public forum. But maybe I wasn't clear enough. And the first poster want a way to pass and receive multiple parameters. So, let's try to solve his problem...

Suppose that I have two forms, named Form1 and Form2. The second one is modal.

On the first form I have a command button. Here's the Click event code:

DIMENSION arrMatrice[1,3]
DO FORM FORM2 WITH arrMatrice TO arrMatrice[1,3]

In the Init Event of the second's form:

Parameters arrMatrice[1,3]
Thisform.arrMatrice[1,3] = arrMatrice[1,3] &amp;&amp; I've created a property in the form

On the second form I have a command button. Here's the click event:

THISFORM.arrMatrice[1,1] = ThisForm.Text1.Value
THISFORM.arrMatrice[1,2] = ThisForm.Text2.Value
THISFORM.arrMatrice[1,3] = ThisForm.Text3.Value
* I have filled the array elements using some textboxes,
* but can be anything here
Thisform.release

In Unload Event of the second's form:

arrMatrice[1,1] = thisform.arrMatrice[1,1]
arrMatrice[1,2] = thisform.arrMatrice[1,2]
arrMatrice[1,3] = thisform.arrMatrice[1,3]
* Fill the array with the form's property values
return arrMatrice[1,3] &amp;&amp; pass the array

And in the first form, after the line DO FORM ....
the values of the array are there.


I'm sorry for my incoherence, but my local time is 03:50, and I'm still working... :(


So, using this method, I can return a nearly unlimited
number of values to the first form. And after the release
of the first form, the array is released, also.

Hope I've made myself much clearer, this time,

Have all the best,
Grigore Dolghin
Class Software
 
Is there a way that you can use your second form as a class (from a VCX) iso a form (an SCX). In that case you can use the array property values after hiding the second modal form by directly referring to the array property.

I must say that I prefer using classes, since they are easier to manipulate. Maybe you should consider this option for the described situation.

Weedz
veld4663@exact.nl

'It never hurts to help...' - Eek the cat.
 
I've used this type of code with great success. Should work in both regular forms and classes.

store space(1) to x1
store space(2) to x2
store space(3) to x3
do form test with x1,x2,x3

Init event Form test
paramter x1,x2,x3

store x1 to object1.value
store x2 to object2.value
store x3 to object3.value

Destroy event form test
store object1.value to x1
store object2.value to x2
store object3.value to x3

I've also stored to properties.

I've passed over a dozen variables this way from both prg's and other forms. The only problem I've had is when passing one forms properties to another I've had to run them through variables.

Bill Couture
 
Combine variables into one-mega variable.
ReturnValue=Var1+';'+Var2+';'+Var3

After returning the ReturnValue, split them again using ';' as the delimeter.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top