Public Variable staying in memory ...

  • Thread starter Thread starter Aidy
  • Start date Start date
A

Aidy

I've got a public variable that keeps its value in memory after the
code has stopped executing. What can I do?

Aidy
 
Aidy,

What is the problem? Public variables are designed specifically
to keep their value even when an executing procedure ends.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Chip Says
Aidy,
What is the problem? Public variables are designed specifically
to keep their value even when an executing procedure ends.

The problem is that I've got a counter variable that I need to hold
it's value only during execution. I was expecting the variable to be
re-initialised every time I run the code.

The problem is that if I run 5 tests I get

test 1
test 2
test 3
test 4
test 5

But when I re-run the tests, I get

test 6
test 7
test 8
test 9
test 10

I need the second run to give me test1, test 2, test 3 etc

I cannot change the scope (because the sub is used in different
modules) and cannot re-intialise in the sub, because the sub is called
numerous times in one run and I need the number to increment.

Here is the code

Const LOGFILE = "C:\test.txt"
Public lLogNum As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As
Long)

Public Sub Logit(sLogText As String, slogStatus As String)

'Logging to a sequential file
Dim FileNumber As Long
Dim sRet As String

FileNumber = FreeFile
lLogNum = lLogNum + 1

Open LOGFILE For Append As FileNumber
'Write a header record:
If lLogNum = 1 Then
Write #FileNumber, "Starting Tests on:" & Chr(32) &
Format(Date, "DD-MMM-YYYY")
End If

Write #FileNumber, lLogNum, sLogText, slogStatus, Format(Now,
"HH:MM:SS")

Close FileNumber

End Sub


Cheers

Aidy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top