Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Remove object from memory

Maybe stupid question, but I couldn't find an answer in the help file
or the forum:
How do I remove an object from memory?
I am looking for the opposite of New :
Set MyObject = New MyClass
Now how do I free the memory occupied by this object ?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,089
Default Remove object from memory

Set MyObject = Nothing

Regards

Trevor


"lexcel" wrote in message
oups.com...
Maybe stupid question, but I couldn't find an answer in the help file
or the forum:
How do I remove an object from memory?
I am looking for the opposite of New :
Set MyObject = New MyClass
Now how do I free the memory occupied by this object ?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Remove object from memory

Hi Lexcel,

Try:

Set myOjrct = Nothing


---
Regards,
Norman



"lexcel" wrote in message
oups.com...
Maybe stupid question, but I couldn't find an answer in the help file
or the forum:
How do I remove an object from memory?
I am looking for the opposite of New :
Set MyObject = New MyClass
Now how do I free the memory occupied by this object ?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Remove object from memory


Thanks for the reply, but does this really remove the object from
memory as well?
What if I did the following:

Dim Fred as MyObject, George as New MyObject

Set Fred = George
Set George = Nothing

I could still access the same object through Fred, it is not erased
from memory.
For many built-in functions there is a Delete method, that is more like
what I am looking for.
Maybe may question should be:
How do I create a Delete method which frees the memory occupied by my
object?

With regards,

Lex

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,089
Default Remove object from memory

Lex

All you've done is created a reference. Then you've put some data there.
You've used it and then you've set it to nothing ... cleared it out.

Try:

Sub test()
Dim MyObject As Worksheet
Set MyObject = Worksheets(1)
MsgBox MyObject.Name
Set MyObject = Nothing
MsgBox MyObject.Name
End Sub

and see what happens.

Regards

Trevor


"lexcel" wrote in message
ups.com...

Thanks for the reply, but does this really remove the object from
memory as well?
What if I did the following:

Dim Fred as MyObject, George as New MyObject

Set Fred = George
Set George = Nothing

I could still access the same object through Fred, it is not erased
from memory.
For many built-in functions there is a Delete method, that is more like
what I am looking for.
Maybe may question should be:
How do I create a Delete method which frees the memory occupied by my
object?

With regards,

Lex





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Remove object from memory

Hi Lexcel,

See Chip Pearson's comments in the section entitled: 'Don't Use The New
Keyword In A Dim Statement'

http://www.cpearson.com/excel/variables.htm


---
Regards,
Norman



"lexcel" wrote in message
ups.com...

Thanks for the reply, but does this really remove the object from
memory as well?
What if I did the following:

Dim Fred as MyObject, George as New MyObject

Set Fred = George
Set George = Nothing

I could still access the same object through Fred, it is not erased
from memory.
For many built-in functions there is a Delete method, that is more like
what I am looking for.
Maybe may question should be:
How do I create a Delete method which frees the memory occupied by my
object?

With regards,

Lex



  #7   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Remove object from memory

I remember reading somewhere that an object won't be removed from memory
until ALL references to the object are set to nothing - which is what I think
Trevor is getting at.

"lexcel" wrote:


Thanks for the reply, but does this really remove the object from
memory as well?
What if I did the following:

Dim Fred as MyObject, George as New MyObject

Set Fred = George
Set George = Nothing

I could still access the same object through Fred, it is not erased
from memory.
For many built-in functions there is a Delete method, that is more like
what I am looking for.
Maybe may question should be:
How do I create a Delete method which frees the memory occupied by my
object?

With regards,

Lex


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Remove object from memory

Thanks Norman, I was aware of this article, but as this is example code
which will never be compiled I thought efficiency was of minor
importance.
But maybe the example would have been more clear if I put the New
outside the Dim.
Roaming the internet I find the most peculiar and interesting
information, but still no answer... Only more questions. I'll put them
on the forum and see what happens.

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Remove object from memory

I got it.
I am a bit ashamed as well.
But I didn't believe that VBA was so complex (to make our lives
simpler).
I added a Class_Terminate routine to MyClass and indeed, it was not
called as long as at least 1 pointer to the object survived.
So VBA inserts code that keeps track of the number of pointers to an
object and when that becomes 0 deletes the object. That implies that it
is up to VBA to remove the object from memory or not and the programmer
has no control over it.
But it makes the programmers life more simple, no worry about cleaning
up.

Anyway it is clear now what happens and everybody was right.

Thanks a lot.

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
How can I remove a calendar object I inserted in a spreadsheet? Buddy M Excel Discussion (Misc queries) 4 October 27th 08 05:25 PM
How do I remove an embeded flash object from a spreadsheet? WireManDan Excel Discussion (Misc queries) 1 May 18th 08 05:34 PM
Is it possible to Shell and remove Excel from memory? alainB Excel Discussion (Misc queries) 3 August 2nd 06 04:16 PM
How to remove a drawn chart object using VBA. Raas Excel Programming 3 November 22nd 05 04:04 PM
Remove hyperlink from a graphical object v_gyku Excel Programming 0 September 21st 05 10:24 AM


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

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

About Us

"It's about Microsoft Excel"