ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   set Object = Nothing (https://www.excelbanter.com/excel-programming/414315-set-object-%3D-nothing.html)

Ardus Petus

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



Peter T

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





ML0940

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




RB Smissaert

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





Charlie

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.




All times are GMT +1. The time now is 03:12 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com