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

Functions and variable scope

Status
Not open for further replies.

travisbrown

Technical User
Dec 31, 2001
1,016
0
0
If you call a function within a function, are variables kept within scope, or can they conflict with the inner function if named the same? In this case, should the closing of the inner RS inadvertently close the outer RS? I mean, it seems to be doing this, but just making sure that it should do this. I thought variables would be kept in scope only.

Code:
FUNCTION TimeOff(id, StartDate, EndDate)
	sSQL = "Select..."
					  
	Set rs = Server.CreateObject("ADODB.Recordset")
	rs.ActiveConnection = connSTRING
	rs.Source = sSQL
	rs.Open()
	IF NOT rs.EOF OR NOT rs.BOF THEN
	 TimeOff = "A Value" & TimeOffDates(id, StartDate)
	END IF
	rs.Close()
	Set rs = Nothing
END FUNCTION

FUNCTION TimeOffDates(ContactID, StartDate)
	sSQL = "SELECT ..."
	Set rs = Server.CreateObject("ADODB.Recordset")
	rs.ActiveConnection = connSTRING
	rs.Source = sSQL
	rs.Open()
	IF NOT rs.EOF OR NOT rs.BOF THEN
		TimeOffDates = "Another value"
	END IF
	rs.Close()
	Set rs = Nothing
END FUNCTION
 
make two different rs connections, its better for performance reason anyways

Army : Combat Engineer : 21B

 
The variables are in seperate scopes if passing by value (which is default). However, if passing by reference, then the inner function will overwrite the values of the variable in the second function.

To be sure, I like to explicitly pass my function parameters, i.e.:
Code:
Function myFunction(ByVal test_1, ByRef test_2)
    [...]
End Function

Take Care,
Mike
 
Michael, yes I'm familiar with passing by reference. I'm talking about variables set inside the function. RS is closed in the inner function, and apparently closes the rs in the outer function (or any RS named rs). Makes sense, but I happily assumed that it would have been discrete.
 
It all boils down to where you declared your variables. Also, you should not close your RS inside another function from the one that opened it. Always close th RS in the same function that opens it (as a rule, not specific to your code).

In reviewing your code now, I am guessing that you are asking about the RS variables in particular. They will not override each other, since each is declared within its own function. Your code will not conflict with each other as it is in your example.

Take Care,
Mike
 
It seems that they do override. They are open and closed within their own function but if named the same, the inner rs closes the outer rs. If they are named the same, I get this, with 255 being the closing of the rs in the outer function.

Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required: 'rs'
/cops/incFunctions.asp, line 255

If I change it as below, it works.

Code:
FUNCTION TimeOff(id, StartDate, EndDate)
    sSQL = "Select..."
                      
    Set rs = Server.CreateObject("ADODB.Recordset")
    rsTimeOff.ActiveConnection = connSTRING
    rsTimeOff.Source = sSQL
    rsTimeOff.Open()
    IF NOT rsTimeOff.EOF OR NOT rsTimeOff.BOF THEN
     TimeOff = "A Value" & TimeOffDates(id, StartDate)
    END IF
    rsTimeOff.Close() <--255
    Set rsTimeOff = Nothing
END FUNCTION

FUNCTION TimeOffDates(ContactID, StartDate)
    sSQL = "SELECT ..."
    Set rs = Server.CreateObject("ADODB.Recordset")
    rs.ActiveConnection = connSTRING
    rs.Source = sSQL
    rs.Open()
    IF NOT rs.EOF OR NOT rs.BOF THEN
        TimeOffDates = "Another value"
    END IF
    rs.Close()
    Set rs = Nothing
END FUNCTION

So, I guess the question is, is the scope of open connections for the entire current script? Only one rs per name can be open/closed even if contained by function?
 
I think that is because you did not DIM the rs within the function. VB will then assume that it is a global variable (I believe). Try it with the same name when dimming rs inside the function.

Take Care,
Mike
 
as this is the ASP forum, therefore VbScript may I gently point out there isn't a ByVal / ByRef option. That's for VB






Unless of course this is a VB question in which case ...

Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
as this is the ASP forum, therefore VbScript may I gently point out there isn't a ByVal / ByRef option. That's for VB"

Sure there is. I use it everyday in vbs/asp.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top