Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 718
Default set Object = Nothing

I see quite often Excel Sub's with following code

Sub mySub()
Dim myObject as Object
set myObject = CreateObject("www.xx")
...
(some code)
...
set myObject = Nothing
end Sub

What is the use of setting a local variable to Nothing, as it will be
destryed by End Sub ?

Cheers,
--
AP


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default set Object = Nothing

In your particular example I do not believe there is any need to explicitly
destroy the object. As you say it will go out of scope when the routine
terminates.

Regards,
Peter T


"Ardus Petus" wrote in message
...
I see quite often Excel Sub's with following code

Sub mySub()
Dim myObject as Object
set myObject = CreateObject("www.xx")
...
(some code)
...
set myObject = Nothing
end Sub

What is the use of setting a local variable to Nothing, as it will be
destryed by End Sub ?

Cheers,
--
AP




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 47
Default set Object = Nothing


Actually, when you declare a variable, you are basically reserving a chunk
of memory in your computer to store that variable (read about Data Types) so
by setting it to nothing, you may be freeing that memory (resources) up on
your computer

Also, by setting the variable to Nothing, you are letting go on the
connection to the object that you set the variable to; especially in this
case to where it is not an intrinsic Excel object but rather a external
object.

So, espeically in this case, I def. say it is a very good idea.

Suppose your macro crashes prior to getting to the End Sub?

It is never a bad practice to let go of objects when you are done with them
but there can be some problems on occasion, if you do not.

There are also occasions to where I will set a string variable to "" when
done with it. That depends on the case and need but with external conections,
I would say ALWAYS set it free when done with it; otherwise a real crash can
take place

Hope this helps a bit

ML


"Ardus Petus" wrote:

I see quite often Excel Sub's with following code

Sub mySub()
Dim myObject as Object
set myObject = CreateObject("www.xx")
...
(some code)
...
set myObject = Nothing
end Sub

What is the use of setting a local variable to Nothing, as it will be
destryed by End Sub ?

Cheers,
--
AP



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default set Object = Nothing

Suppose your macro crashes prior to getting to the End Sub?

Wouldn't that release all variables in any case?

RBS


"ML0940" wrote in message
...

Actually, when you declare a variable, you are basically reserving a chunk
of memory in your computer to store that variable (read about Data Types)
so
by setting it to nothing, you may be freeing that memory (resources) up on
your computer

Also, by setting the variable to Nothing, you are letting go on the
connection to the object that you set the variable to; especially in this
case to where it is not an intrinsic Excel object but rather a external
object.

So, espeically in this case, I def. say it is a very good idea.

Suppose your macro crashes prior to getting to the End Sub?

It is never a bad practice to let go of objects when you are done with
them
but there can be some problems on occasion, if you do not.

There are also occasions to where I will set a string variable to "" when
done with it. That depends on the case and need but with external
conections,
I would say ALWAYS set it free when done with it; otherwise a real crash
can
take place

Hope this helps a bit

ML


"Ardus Petus" wrote:

I see quite often Excel Sub's with following code

Sub mySub()
Dim myObject as Object
set myObject = CreateObject("www.xx")
...
(some code)
...
set myObject = Nothing
end Sub

What is the use of setting a local variable to Nothing, as it will be
destryed by End Sub ?

Cheers,
--
AP




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default set Object = Nothing


"ML0940" wrote:

There are also occasions to where I will set a string variable to "" when
done with it. That depends on the case and need ...


Really? A local string variable? Right before exiting the sub? Just
exactly when would there ever be a need for that? Don't worry, after 2 or 3
decades of including unnecessary lines of code you'll either get over it or
move into management.


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
2 Label Options - Forms Object vs Control Box Object Awrex Excel Discussion (Misc queries) 3 July 17th 09 07:10 PM
Option button object proeprties or object not found in vba Pete Straman S via OfficeKB.com Excel Programming 0 August 31st 05 05:49 PM
Confusion about how the Window object fits into the Excel object model Josh Sale Excel Programming 11 April 15th 05 06:08 PM
returning pivottable object from a range object Grant Excel Programming 2 September 27th 04 02:22 AM
Range object to Array object conversion Tom Ogilvy Excel Programming 0 August 1st 03 12:16 AM


All times are GMT +1. The time now is 02:38 PM.

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"