![]() |
Do I need to destroy a collection I made when I am done with it?
I have some code that intializes a new collection, runs code using the
collection and then ends. Do I need to destroy the collection so it doesn't take up memory or does this automatically happen when the procedure is over? Is there a way to see what is in memory? Option Explicit Private Sub cboMounting_Change() Dim colPoleSpecsAll As Collection Dim colPoleSpecsData As Collection Dim ctrl As Control Set colPoleSpecsAll = New Collection With colPoleSpecsAll .Add lblPoles .Add cboPoles .Add lblShape .Add cboShape .Add lblFieldPoleSize .Add cboFieldPoleSize End With Set colPoleSpecsData = New Collection With colPoleSpecsData .Add cboPoles .Add cboShape .Add cboFieldPoleSize End With If cboMounting.ListIndex 1 Then For Each ctrl In colPoleSpecsAll ctrl.Enabled = True Next ctrl For Each ctrl In colPoleSpecsData ctrl.BackColor = vbWindowBackground Next ctrl Else For Each ctrl In colPoleSpecsAll ctrl.Enabled = False Next ctrl For Each ctrl In colPoleSpecsData ctrl.BackColor = vbButtonFace Next ctrl End If End Sub -- Cheers, Ryan |
Do I need to destroy a collection I made when I am done with it?
On Sep 3, 1:34*pm, RyanH wrote:
I have some code that intializes a new collection, runs code using the collection and then ends. *Do I need to destroy the collection so it doesn't take up memory or does this automatically happen when the procedure is over? * Is there a way to see what is in memory? Option Explicit Private Sub cboMounting_Change() Dim colPoleSpecsAll As Collection Dim colPoleSpecsData As Collection Dim ctrl As Control * * Set colPoleSpecsAll = New Collection * * * * With colPoleSpecsAll * * * * * * .Add lblPoles * * * * * * .Add cboPoles * * * * * * .Add lblShape * * * * * * .Add cboShape * * * * * * .Add lblFieldPoleSize * * * * * * .Add cboFieldPoleSize * * * * End With * * Set colPoleSpecsData = New Collection * * * * With colPoleSpecsData * * * * * * .Add cboPoles * * * * * * .Add cboShape * * * * * * .Add cboFieldPoleSize * * * * End With * * If cboMounting.ListIndex 1 Then * * * * For Each ctrl In colPoleSpecsAll * * * * * * ctrl.Enabled = True * * * * Next ctrl * * * * For Each ctrl In colPoleSpecsData * * * * * * ctrl.BackColor = vbWindowBackground * * * * Next ctrl * * Else * * * * For Each ctrl In colPoleSpecsAll * * * * * * ctrl.Enabled = False * * * * Next ctrl * * * * For Each ctrl In colPoleSpecsData * * * * * * ctrl.BackColor = vbButtonFace * * * * Next ctrl * * End If End Sub -- Cheers, Ryan Procedure level variables are destroyed when the procedure finishes, however it's often thought of as good practice to destroy any objects as soon as they become redundant - this is especially the case when there's error handling involved etc. The locals window in the VBE will tell you if your variables are in use or not. |
Do I need to destroy a collection I made when I am done with i
Cool! So my collection is destroyed when the procedure ends? If I need to
destroy a variable, how do I do that? Kill VariableName? -- Cheers, Ryan "Alex Simmons" wrote: On Sep 3, 1:34 pm, RyanH wrote: I have some code that intializes a new collection, runs code using the collection and then ends. Do I need to destroy the collection so it doesn't take up memory or does this automatically happen when the procedure is over? Is there a way to see what is in memory? Option Explicit Private Sub cboMounting_Change() Dim colPoleSpecsAll As Collection Dim colPoleSpecsData As Collection Dim ctrl As Control Set colPoleSpecsAll = New Collection With colPoleSpecsAll .Add lblPoles .Add cboPoles .Add lblShape .Add cboShape .Add lblFieldPoleSize .Add cboFieldPoleSize End With Set colPoleSpecsData = New Collection With colPoleSpecsData .Add cboPoles .Add cboShape .Add cboFieldPoleSize End With If cboMounting.ListIndex 1 Then For Each ctrl In colPoleSpecsAll ctrl.Enabled = True Next ctrl For Each ctrl In colPoleSpecsData ctrl.BackColor = vbWindowBackground Next ctrl Else For Each ctrl In colPoleSpecsAll ctrl.Enabled = False Next ctrl For Each ctrl In colPoleSpecsData ctrl.BackColor = vbButtonFace Next ctrl End If End Sub -- Cheers, Ryan Procedure level variables are destroyed when the procedure finishes, however it's often thought of as good practice to destroy any objects as soon as they become redundant - this is especially the case when there's error handling involved etc. The locals window in the VBE will tell you if your variables are in use or not. |
Do I need to destroy a collection I made when I am done with i
All procedure level objects and varaibles are stored on the stack. When the
procedure ends the objects and variables are removed from the stack. Think of it like building blocks where you can add and remove block. The blocks in this case are memory blocks. When a procedure initializes it creates the blocks. When the procedure ends the blocks are taken off of the stack. The reason that it is considered to be good coding practice to destroy the objects is that the entire object is not stored on the stack. There is just a pointer to a different memory address where the object resides. When the procedure ends and the pointer is destroyed the actual object is also supposed to be destroyed. If for some reason the object is not destroyed then you have a memory leak. You have an object in memory with absolutely no way to access it. In practice I don't tend to set my objects back to nothing when I am done. I trust that the program destroys the Note that this is different from Heap memory where your constants and globals are stored. That is similar to the stack but the memory blocks are added and removed when the program starts and ends. -- HTH... Jim Thomlinson "RyanH" wrote: Cool! So my collection is destroyed when the procedure ends? If I need to destroy a variable, how do I do that? Kill VariableName? -- Cheers, Ryan "Alex Simmons" wrote: On Sep 3, 1:34 pm, RyanH wrote: I have some code that intializes a new collection, runs code using the collection and then ends. Do I need to destroy the collection so it doesn't take up memory or does this automatically happen when the procedure is over? Is there a way to see what is in memory? Option Explicit Private Sub cboMounting_Change() Dim colPoleSpecsAll As Collection Dim colPoleSpecsData As Collection Dim ctrl As Control Set colPoleSpecsAll = New Collection With colPoleSpecsAll .Add lblPoles .Add cboPoles .Add lblShape .Add cboShape .Add lblFieldPoleSize .Add cboFieldPoleSize End With Set colPoleSpecsData = New Collection With colPoleSpecsData .Add cboPoles .Add cboShape .Add cboFieldPoleSize End With If cboMounting.ListIndex 1 Then For Each ctrl In colPoleSpecsAll ctrl.Enabled = True Next ctrl For Each ctrl In colPoleSpecsData ctrl.BackColor = vbWindowBackground Next ctrl Else For Each ctrl In colPoleSpecsAll ctrl.Enabled = False Next ctrl For Each ctrl In colPoleSpecsData ctrl.BackColor = vbButtonFace Next ctrl End If End Sub -- Cheers, Ryan Procedure level variables are destroyed when the procedure finishes, however it's often thought of as good practice to destroy any objects as soon as they become redundant - this is especially the case when there's error handling involved etc. The locals window in the VBE will tell you if your variables are in use or not. |
Do I need to destroy a collection I made when I am done with i
So I need to set object variables to Nothing and other variables to Empty?
Or is there a kill variable function? -- Cheers, Ryan "Jim Thomlinson" wrote: All procedure level objects and varaibles are stored on the stack. When the procedure ends the objects and variables are removed from the stack. Think of it like building blocks where you can add and remove block. The blocks in this case are memory blocks. When a procedure initializes it creates the blocks. When the procedure ends the blocks are taken off of the stack. The reason that it is considered to be good coding practice to destroy the objects is that the entire object is not stored on the stack. There is just a pointer to a different memory address where the object resides. When the procedure ends and the pointer is destroyed the actual object is also supposed to be destroyed. If for some reason the object is not destroyed then you have a memory leak. You have an object in memory with absolutely no way to access it. In practice I don't tend to set my objects back to nothing when I am done. I trust that the program destroys the Note that this is different from Heap memory where your constants and globals are stored. That is similar to the stack but the memory blocks are added and removed when the program starts and ends. -- HTH... Jim Thomlinson "RyanH" wrote: Cool! So my collection is destroyed when the procedure ends? If I need to destroy a variable, how do I do that? Kill VariableName? -- Cheers, Ryan "Alex Simmons" wrote: On Sep 3, 1:34 pm, RyanH wrote: I have some code that intializes a new collection, runs code using the collection and then ends. Do I need to destroy the collection so it doesn't take up memory or does this automatically happen when the procedure is over? Is there a way to see what is in memory? Option Explicit Private Sub cboMounting_Change() Dim colPoleSpecsAll As Collection Dim colPoleSpecsData As Collection Dim ctrl As Control Set colPoleSpecsAll = New Collection With colPoleSpecsAll .Add lblPoles .Add cboPoles .Add lblShape .Add cboShape .Add lblFieldPoleSize .Add cboFieldPoleSize End With Set colPoleSpecsData = New Collection With colPoleSpecsData .Add cboPoles .Add cboShape .Add cboFieldPoleSize End With If cboMounting.ListIndex 1 Then For Each ctrl In colPoleSpecsAll ctrl.Enabled = True Next ctrl For Each ctrl In colPoleSpecsData ctrl.BackColor = vbWindowBackground Next ctrl Else For Each ctrl In colPoleSpecsAll ctrl.Enabled = False Next ctrl For Each ctrl In colPoleSpecsData ctrl.BackColor = vbButtonFace Next ctrl End If End Sub -- Cheers, Ryan Procedure level variables are destroyed when the procedure finishes, however it's often thought of as good practice to destroy any objects as soon as they become redundant - this is especially the case when there's error handling involved etc. The locals window in the VBE will tell you if your variables are in use or not. |
Do I need to destroy a collection I made when I am done with i
When the procedure ends so does the memory associated with the variables in
the procedure so you do not need to do anything... How it works is when your program starts a block of memory is set aside for your stack. For argument sake lets call it a meg. Whether you have declared 1 variable or 100 variables there is 1 meg set aside. If you get into an infinite loop where you are declaring variables sometimes you can get a stack overflow error. That just means that you have used the entire meg and there is no more space. The actual memory management is a bit more complicated than that but that is the jist of it. -- HTH... Jim Thomlinson "RyanH" wrote: So I need to set object variables to Nothing and other variables to Empty? Or is there a kill variable function? -- Cheers, Ryan "Jim Thomlinson" wrote: All procedure level objects and varaibles are stored on the stack. When the procedure ends the objects and variables are removed from the stack. Think of it like building blocks where you can add and remove block. The blocks in this case are memory blocks. When a procedure initializes it creates the blocks. When the procedure ends the blocks are taken off of the stack. The reason that it is considered to be good coding practice to destroy the objects is that the entire object is not stored on the stack. There is just a pointer to a different memory address where the object resides. When the procedure ends and the pointer is destroyed the actual object is also supposed to be destroyed. If for some reason the object is not destroyed then you have a memory leak. You have an object in memory with absolutely no way to access it. In practice I don't tend to set my objects back to nothing when I am done. I trust that the program destroys the Note that this is different from Heap memory where your constants and globals are stored. That is similar to the stack but the memory blocks are added and removed when the program starts and ends. -- HTH... Jim Thomlinson "RyanH" wrote: Cool! So my collection is destroyed when the procedure ends? If I need to destroy a variable, how do I do that? Kill VariableName? -- Cheers, Ryan "Alex Simmons" wrote: On Sep 3, 1:34 pm, RyanH wrote: I have some code that intializes a new collection, runs code using the collection and then ends. Do I need to destroy the collection so it doesn't take up memory or does this automatically happen when the procedure is over? Is there a way to see what is in memory? Option Explicit Private Sub cboMounting_Change() Dim colPoleSpecsAll As Collection Dim colPoleSpecsData As Collection Dim ctrl As Control Set colPoleSpecsAll = New Collection With colPoleSpecsAll .Add lblPoles .Add cboPoles .Add lblShape .Add cboShape .Add lblFieldPoleSize .Add cboFieldPoleSize End With Set colPoleSpecsData = New Collection With colPoleSpecsData .Add cboPoles .Add cboShape .Add cboFieldPoleSize End With If cboMounting.ListIndex 1 Then For Each ctrl In colPoleSpecsAll ctrl.Enabled = True Next ctrl For Each ctrl In colPoleSpecsData ctrl.BackColor = vbWindowBackground Next ctrl Else For Each ctrl In colPoleSpecsAll ctrl.Enabled = False Next ctrl For Each ctrl In colPoleSpecsData ctrl.BackColor = vbButtonFace Next ctrl End If End Sub -- Cheers, Ryan Procedure level variables are destroyed when the procedure finishes, however it's often thought of as good practice to destroy any objects as soon as they become redundant - this is especially the case when there's error handling involved etc. The locals window in the VBE will tell you if your variables are in use or not. |
All times are GMT +1. The time now is 05:53 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com