[AccessD] Public Variables - Scope and Lifetime

Arthur Fuller artful at rogers.com
Sat Nov 26 21:41:26 CST 2005


Okay... Since I never use globals, I decided to write a tester to verify
your assertion.
<code>
Option Compare Database
Option Explicit
Global gstrTest As String

Function TestGlobal() As Long
    gstrTest = "This is a test"
    MsgBox gstrTest
    Stop
    Call Proc1
    MsgBox gstrTest
    Call Proc2
End Function


Private Sub Proc1()
    MsgBox gstrTest, vbOKOnly, "Proc1"
    Stop
End Sub

Private Sub Proc2()
    MsgBox gstrTest, vbOKOnly, "Proc2"
    Stop
End Sub
</code>
Which leads me to wonder what Drew meant in the foregoing message:
"Ugh....not this again!  LOL.  Marty, that is not what happens.  If you STOP
your code, then everything gets reset.  That does include your global
variables.  It's not a maybe, and they don't 'lose' their values, the code
is stopped.  It is just like stopping an .exe, all values in memory are
cleared."
Just to make sure, at each STOP statement, I went into the Debug window and
entered "Debug.Print gstrTest" and obtained the expected result. So in this
respect, it would appear that globals and statics are equivalent. So then
what was Drew getting at? I could easily be missing the point; it certainly
wouldn't be the first time.
Arthur
-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Stuart McLachlan
Sent: November 26, 2005 7:48 PM
To: Access Developers discussion and problem solving
Subject: Re: [AccessD] Public Variables - Scope and Lifetime

On 26 Nov 2005 at 16:46, Arthur Fuller wrote:

> Your reply made me decide to test static functions against a STOP, which I
> have never done before.
> Turns out that static values are preserved despite a STOP. Code follows. 

No different to a global variable, this works the same way:

Function TestGlobal() As Long
gstrTest = "This is a test"
 MsgBox gstrTest
Stop
MsgBox gstrTest
End Function

-- 
Stuart


-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com




More information about the AccessD mailing list