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

Multiple Values back from a function 1

Status
Not open for further replies.
Joined
Jun 17, 2004
Messages
73
Location
US
How would I go about returning multiple values back from a function? I want to be able to get the POS and TOS values.

This is in the body.
Call BCBSADJUSTMENT(Insurance)



Function BCBSADJUSTMENT(Insurance)

If Insurance = "BCFL-ELE" Then
POS = "ADSBC"
TOS = "11"
Else
POS = "ADS"
TOS = ""
End if
 
If you declare the variables in the body of the page, not inside a function, then they are global and any modifications you make to them inside a function will remain outside the function.

Lee

Dim TOS, POS

Sub BCBSADJUSTMENT(Insurance)

If Insurance = "BCFL-ELE" Then
POS = "ADSBC"
TOS = "11"
Else
POS = "ADS"
TOS = ""
End if

End Sub

BCBSADJUSTMENT Insurance

Response.Write POS 'shows either ADS or ADSBC
Response.Write TOS 'shows either 11 or an empty string

 
Or you could build a class to wrap all the return values:
Code:
Class BCBSReturnValue
   Public POS, TOS
End Class

Sub BCBSADJUSTMENT(Insurance)

Set BCBSADJUSTMENT = New BCBSReturnValue
If Insurance = "BCFL-ELE" Then
    BCBSADJUSTMENT.POS = "ADSBC"
    BCBSADJUSTMENT.TOS = "11"
Else
    BCBSADJUSTMENT.POS = "ADS"
    BCBSADJUSTMENT.TOS = ""
End if

End Sub

Dim Adjustment
Set Adjustment = BCBSADJUSTMENT(Insurance)

Response.Write Adjustment.POS 'shows either ADS or ADSBC
Response.Write Adjustment.TOS 'shows either 11 or an empty string

Just a thought :) (does that word - thought - look wrong to anyone else or is my coffee level low?)

-T

barcode_1.gif
 
Can you assign the same variable name to an instance of an object as well as a SUB in VBScript? I work mainly with JScript, and haven't done much with objects in VBScript.

Lee

 
Thanks everyone. Ended up going with trollacious suggestion worked great.
 
troll: VBScript has this wierd practice for return values. Basically you return a value by assigning it to the function name. You can't actually assign a refernce to a sub/function to a variable, it just looks like it here :P

So basically saying something like:
FunctionName = someValue
is the same as saying
return someValue;

except that in VBScript the function doesn't exit when you assign that value, so you can actually treat it as a variable until the code exits and it returns whatever is left as the return value of the function. So:
Code:
Function Bob()
   Bob = "123"
   Bob = Bob & "456"
End Function
Bob would return "123456" because it doesn't exit when you set the return value. Basically consider that variable to be an intermediary until the absolute last line. Unless you explicitly end the function it continues on till it reaches it's natural end.

Not sure if that answere your quesiton, I got momentarily sidetracked by a phone call :P

-T

barcode_1.gif
 
I work with VB 6 (and with QuickBASIC since 1991), so was aware of assigning values to the function name, just hadn't run across assigning an instance of an object to one. Your example was the first time I've seen it used for more than an intrinsic data type or a Type.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top