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!

Pass Value of a var in a sub to another sub

Status
Not open for further replies.

dwg23

Technical User
Oct 21, 2002
151
US
I know this is a no-brainer but I can't seem to make it happen.
I have a sub that generates a value and it is put into a var called playera
playera is defined globaly.(top of page dim playera)

When I try to use playera in another sub it has no value.
<%sub getpayment
if playera = "1" then
amountdue = "500"
end if
response.write playera%> (I get nothing)
How do I get the value of playera into the new sub?
 

Can you show us all the code that relates to this... you don't show us where you define the var, or the sub, etc,etc... It seems like this is only part of the code...

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Sure,

'top of page

<%dim aplayer and a lot of other vars%>

'code to setup page

<%sub DisplayLevel6
set DB = server.createobject("ADODB.Connection")
DB.Open constring
set rs = server.CreateObject("ADODB.Recordset")
Sql = "SELECT APaid, BPaid"
sql = sql & " FROM Doubles"
sql = sql & " where AID = " & clng(strPlayerBID)
sql = sql & " or BID = " & clng(strPlayerBID)
RS.Open Sql,DB,adOpenDynamic
if rs("apaid") <> "" or rs("bpaid") <> "" then
aplayer = "1"
end if
end sub%>

More code for page

<% sub GetPayment
aplay = aplayer
aplay = aplayer
bplay = bplayer
if aplay = "1" then
num = strPlayerBID
else
num = strPlayerAID
end if
if aplay = "" and bplay = "" then
num = strPlayerAID & " " & strPlayerBID
end if
if aplay = "1" then
num = strPlayerBID
else
num = strPlayerAID
end if
if aplay = "" and bplay = "" then
num = strPlayerAID & " " & strPlayerBID
end if%>


If I do a response.write for aplay or aplayer I get nothing.
There is more code for bplayer but it is the same as for aplayer so I left it out.
Thanks
Dave
 
If you want to "pass" variable by thinking global scope, you have to specifically dim it at the top level.
[tt]
<%
dim aplayer, bplayer
sub DisplayLevel6
'must not have explicit statement of dim aplayer, bplayer inside this sub
'etc etc
end sub
%>
<%
sub GetPayment
'same requirement: must not have explicit statement of dim aplayer, bplayer inside this sub
'etc etc
end sub
%>
[/tt]
But you will invite criticism on too much relying on the default design and invite recommendation of using option explicit directive for more rigor.
 
I'm confused. I don't see an End Sub declaration and I'm not sure which variable your trying to re-use. If you only need to re-use a single variable you could turn your sub into a Function which is designed to return a variable. Global variables are generally considered bad practice because they could potentially be changed anywhere in the code and then used by a later portion of the code without any idea whether they were last set were you expected it to be set or were altered by another piece of your code.

Second issue, you are also depending on global variables inside your sub. Same argument as before except in this case
using the global variables is really unnecessary because subs and functions can take values as arguments. Is there a major difference to passing them as variables vs using global variables? Nope, except that by using variables you can easily test your function with hard-coded values, the code is easier to understand, the function is more re-useable by other portions of your code by passing in other variables as the starting values, etc.

Example of using a function to return a value:
Code:
'To return a value from inside a function, assign it to the function name
Function GetPayment(aplay, bplay, AID, BID)
   If aplay = "1" Then
      GetPayment = AID
   ElseIf aply = "" and bplay = "" Then
      GetPayment = AID & BID
   Else
      GetPayment = BID
   End If
End Function

Dim payval
payval = GetPayment(aplayer, bplayer, strPlayerAID, strPlayerBID)
Response.Write "your payment is " & payval

payval = GetPayment(1,0,100,0)
Response.Write "your hard-coded payment is " & payval

I don't fully understand the strings your using, unless those are id's back to bonuses or something, but the function above should work as you were expecting with the added bonus of being a little easier to understand, re-usable, and able to be used elsewhere in your code without copying and pasting it to change a couple variables.

-T

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top