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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning 2 variables? 1

Status
Not open for further replies.

redzombi5k

Programmer
Jan 21, 2009
22
US
Hi guys,
I know this is a easy one for most of you here, but I cant for the life of me figure out how return 2 variables at the end of a method.

I currently am passing both of them in as lparameters, and then at the end of the code i am saying
Return var1, var2

But I get the 'syntax error' when I try to close that code window so I know I must but doing this wrong. Any help would be much apprieciated!

-Steve
 
Any takers at all?
Iv tried all sorts of combos, like @ before var and & befor var, am I anywhere close?
 
As far as I know, 'return' only returns one variable. Not arrays.

I have always used the '@' syntax on call to a procedure or function when I wanted that procedure or function to operate by reference, not value. This allows the called procedure to write back its results to the caller.

For multiple variables, use a call like: doprocedure (@var1,@var2,@var3,..) or doprocedure (@array) or doprocedure(@object). In all these have the called procedure modify the variable being returned.

 
thanks for the replay, got it all figured out, one more thing I hope im not going to far off the original topic of this post, but when I use strtofile() the file ignores all of my chr(13) (returns) so insted of it being a readable text file it is just one long sentence, any sugestions?
 
Steve,

Glad you've got it figured out.

Just for the record, a method (or function) can only ever return a single value. This is not specifically a FoxPro thing. It's generally true in most programming languages.

To get round this, you have a number of choices:

- Return an object. The properties of the object will contain the values you are interested in.

- Pass parameters, by reference, to the method. Change the values of the parameters within the method. After the method has executed, the caller will see those values.

- Pass an array. Store the values in elements of the array. (Arrays are implicitly passed by reference.)

- Store the values in question in properties of the object that owns the method.

Hope this is of interest.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Try using CHR(13)+CHR(10) since I believe your CHR(13) code is still there, I suspect whatever you're using to display the result it is not displaying it as you expected.

Some displays need only CHR(13) to force a displayed line break (I don't recall which ones offhand) but others require both CHR(13)+CHR(10), commonly referred to as CrLf (CarriageReturn/LineFeed.)
 
Using @ to pass variables by reference is not needed for procedures. By default, parameters are passed by reference to procedures and by by value to functions and methods.

Craig Berntson
MCSD, Visual FoxPro MVP,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top