Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default module-level variable lifetime

This question looks like its been asked several hundred times, but a search
through Google/manuals still hasn't cleared this up for me:

What exactly is the lifetime of module-level variables (standard .bas
modules and thisworkbook module) ??

Heres some test code I put together:

* I create a helper Class Module, class1, that looks like this.

Private Sub Class_Initialize()
MsgBox "Hello world"
End Sub
Private Sub Class_Terminate()
MsgBox "Goodbye"
End Sub

* Now in either a new .BAS module or the workbook module, I create a global
variable.

Public x as class1

* I need to instantiate the variable somehow, so I add the following to the
workbook module.

Private Sub Workbook_Open()
Set x = New Class1
End Sub


NOW,

If I open the workbook, the msgbox tells me that the class has been created.
GOOD.
But when does the class ever get destroyed?
If I close the workbook, I dont get a msgbox.
If I shutdown the application, I dont get a msgbox (although I assume the
Class must be destroyed at this point at least)

The only way I can get the class to Terminate is to set the variable =
Nothing, in the Workbook_BeforeClose event...

So what is going on? Why aren't module variables being released when
workbooks are closed?









Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
CLASS MODULE & SIMPLE MODULE FARAZ QURESHI Excel Discussion (Misc queries) 1 September 7th 07 09:32 AM
STATIC VARIABLE NOT AVAILABLE IN ANOTHER MODULE CAPTGNVR Excel Discussion (Misc queries) 2 February 1st 07 07:30 PM
code in module A to not execute a Worksheet_SelectionChange sub of another module Jack Sons Excel Discussion (Misc queries) 4 December 11th 05 11:52 PM
Sum cells based on a row variable and seperate column variable CheeseHeadTransplant Excel Worksheet Functions 10 September 23rd 05 06:59 PM
why is it saying sheetcnt is "variable not defined" how to do a global variable to share over multiple functions in vba for excel? Daniel Excel Worksheet Functions 1 July 9th 05 03:05 AM


All times are GMT +1. The time now is 03:02 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"