p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Classic ASP Databases (http://p2p.wrox.com/forumdisplay.php?f=62)
-   -   Scope of recordset variables (http://p2p.wrox.com/showthread.php?t=8666)

James Diamond January 28th, 2004 06:33 AM

Scope of recordset variables
 
Hi,

I have some general functions in an ASP include file. One of them is:

------------------------------------------------------
Function GetTable(tablename)

    Dim d
    Dim strSQL

    strSQL = "SELECT * FROM " & tablename

    Set d = Server.CreateObject("ADODB.RecordSet")
    rsData.Open strSQL, dcnDB, adOpenStatic, adLockReadOnly

    Set GetTable = d

End Function
------------------------------------------------------


In my main page, I do this:

' Declare recordset variable
Dim rsData

' Call function
Set rsData = GetTable("Clients")

< code to read and display the records etc...>

' Cleanup
rsData.Close
Set rsData = nothing


Question:

I do the cleanup on the main page (to close the recordset). However, there is no cleanup in the function for the local d variable. I can't cleanup that because if I do, I cannot pass the contents of it back in the statement Set GetTable = d.

Does d cleanup automatically by virtue of going out of scope (because it is a local variable in the function)? Or do I end up with the recordset object d floating around on the server because I have not cleaned it up?

Please teach me about this area... cleanups / scoping / persistence of objects / how a recordset is "passed" from the function to my main page, etc.

Thanks.

James

//##




bmains January 28th, 2004 09:23 AM

I'm not as knowledgable as some of the other individuals that post to this site, but what I usually did is this:
Code:

Set GetTable = d
set d = nothing

I know in the .NET world, the garbage collector automatically recycles unused variables for you, but I think in the ASP/VB6 world, you have to do it. I know VB6 you should set your variables to nothing; otherwise, resources will still be open (I had a problem with, when accessing the Word API's, the Word.exe file would never close, and I would be left with 50 instances!).

I'm not sure how crucial it is in ASP; personally, I make a note of it to always set my variables to nothing in the end, even in my .NET development.

Hope this helps,

Brian

James Diamond January 28th, 2004 09:37 AM

Thanks, Brian.

Can someone give me a second opinion on this issue, please?

James



All times are GMT -4. The time now is 10:35 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.