View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
Jim Cone[_2_] Jim Cone[_2_] is offline
external usenet poster
 
Posts: 1,549
Default Keeping Variables Alive


I declared the public collection in a standard module without the New word
and then created an instance of it in a separate sub.
The collection still reset to nothing when I added the second combobox.
That is new to me.

In any case, I try to avoid public variables.
I will pass a collection object to other subs/function as needed an then
set it to nothing when exiting.
--
Jim Cone
Portland, Oregon USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)



"atpgroups"
wrote in message
On 4 Jun, 02:58, "Jim Cone"
wrote: Any time you are in code module and screwing with it the entire
project can be reset.


Even more complicated than that, it seems...
Does anyone know of a definitive list of things that reset the heap?

For example, creating an OLEObject seems to reset the heap, try the
following as an example. (you need two command buttons linked to the
code). The collection increases in size while you add doubles to it,
then increases in size once more when you add a combobox. Then next
time you add a combobox it is back to count=1

Option Explicit
Public coll As New Collection

Private Sub CommandButton1_Click()
Dim v As OLEObject
Set v = Sheet1.OLEObjects.Add(Classtype:="Forms.Combobox.1 ")
coll.Add v
Sheet1.Range("A1").Value = coll.Count
End Sub

Private Sub CommandButton2_Click()
Dim v As Double
v = Timer
coll.Add v
Sheet1.Range("A1").Value = coll.Count
End Sub