LotusScript variable scope

I have a script library containing the following function:--------------Cut-Here---------------------

'PacaGeneral:

Option Public

Option Declare

%INCLUDE “LSCONST.LSS”

%INCLUDE “LSERR.LSS”

Function ReadConfigDocument As Variant

Dim session As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument



ReadConfigDocument = False



Set db = session.CurrentDatabase



Set doc = db.GetProfileDocument( "TEST", "TEST" )

ReadConfigDocument = True

Delete doc

Delete db

End Function

--------------Cut-Here---------------------

I have an agent that uses the script library. The agent code is…

--------------Cut-Here---------------------

'Change Supplier:

Option Public

Option Declare

Use “PacaGeneral”

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase



Set db = session.CurrentDatabase



'Read the configuration document

If Not ReadConfigDocument() Then

End If



If db Is Nothing Then

	Messagebox "DB is Nothing"

Else

	Messagebox "DB is SET"

End If



Print "Done."

Beep

End Sub

--------------Cut-Here---------------------

It is my understanding that the scope of a variable in a subroutine/function using the “Dim” statement is private.

However, when I excute the code above, the “db” object in the agent is deleted outside the scope of the ReadConfigDocument function upon the return from the function.

Shouldn’t the “db” and “doc” objects in the ReadConfigDocument function be local and private?

Should the “Delete” statements in the function affect the agent objects?

What did I do wrong? or what don’t I understand?

Help!

Subject: RE: LotusScript variable scope

What you’re missing is the effect of the Delete statement and the way object variables work.To make the situation clearer, change the variable names in your Initialize subroutine – the script will work exactly the same. It’s not the fact that the variables have the same names. It’s that they point to the same memory.

You really only have one session, even if you declare multiple session variables. And object variables are really just pointers to the objects. So session.CurrentDatabase gets you a pointer to the same object in memory both times you do it.

Ordinarily, LotusScript does reference counting, deleting objects only when the last pointer to them is set to a different value or goes out of scope. However, when you use the Delete function, you’re telling LotusScript that you don’t care how many references there are to this object, you want it deleted. All current variables that point to that memory are set to Nothing. So that can affect variables in other modules, even though the current module could not refer to those variables directly given the scope rules.