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

In this and other Excel NGs, I see quite often VBA lines like:

sub Foo()
dim myRng as Range
dim myRS as ADODB.Recordset
....
set myRng = Nothing
set myRS = Nothing
End Sub

Is this setting objects to Nothing really useful for local variables that
will be automatically destroyed at the end of the Sub.

TIA

--
AP


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Good Practice

In other words, is it necessary? No. Is it good practice? Guess that
depends on how a person defines good practice.

I learned programming a long time ago, when you had to pay attention to
every little detail because the system did not do much for you. Probably
leads to overkill in my coding these days. But I still see some value in
"completeness" in coding and if I create an object variable I want to destroy
it. I don't want to rely on the system to do that. Funny things can keep
the system from doing what it is supposed to do, sometimes. And we wonder
about things like memory leaks, etc... All of these stem from sloppy coding
(not necessarily yours; often the people who coded the system). So for me
"good practice" is to do everything explicitly whenever possible. But others
may have different ideas. After all, some don't even Dim their variables.
--
- K Dales


"Ardus Petus" wrote:

In this and other Excel NGs, I see quite often VBA lines like:

sub Foo()
dim myRng as Range
dim myRS as ADODB.Recordset
....
set myRng = Nothing
set myRS = Nothing
End Sub

Is this setting objects to Nothing really useful for local variables that
will be automatically destroyed at the end of the Sub.

TIA

--
AP



  #3   Report Post  
Posted to microsoft.public.excel.programming
Ed Ed is offline
external usenet poster
 
Posts: 399
Default Good Practice

As a Good Practice, yes. For ranges, maybe not necessary, until you get
into more complicated coding that runs across several Subs - then things can
get exciting! But if you're in the habit of destroying all objects, then
you'll probably do it for file and application objects, too - leave _those_
hanging around, and life gets downright annoying!

Ed

"Ardus Petus" wrote in message
...
In this and other Excel NGs, I see quite often VBA lines like:

sub Foo()
dim myRng as Range
dim myRS as ADODB.Recordset
...
set myRng = Nothing
set myRS = Nothing
End Sub

Is this setting objects to Nothing really useful for local variables that
will be automatically destroyed at the end of the Sub.

TIA

--
AP




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Good Practice

If the object is declared within the sub, there is no reason to
set it to Nothing at the end of the sub. VBA will destroy the
object when the sub ends.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com

"Ardus Petus" wrote in message
...
In this and other Excel NGs, I see quite often VBA lines like:

sub Foo()
dim myRng as Range
dim myRS as ADODB.Recordset
...
set myRng = Nothing
set myRS = Nothing
End Sub

Is this setting objects to Nothing really useful for local
variables that
will be automatically destroyed at the end of the Sub.

TIA

--
AP




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 718
Default Good Practice

This is precisely what I thought.

Thanks,
--
AP

"Chip Pearson" a écrit dans le message de
...
If the object is declared within the sub, there is no reason to
set it to Nothing at the end of the sub. VBA will destroy the
object when the sub ends.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com

"Ardus Petus" wrote in message
...
In this and other Excel NGs, I see quite often VBA lines like:

sub Foo()
dim myRng as Range
dim myRS as ADODB.Recordset
...
set myRng = Nothing
set myRS = Nothing
End Sub

Is this setting objects to Nothing really useful for local
variables that
will be automatically destroyed at the end of the Sub.

TIA

--
AP








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
And in case I don't see you,good afternoon,good evening,and good n msnyc07 Excel Discussion (Misc queries) 1 June 1st 10 11:24 AM
Which folder - good practice? Sunbank Excel Programming 3 February 14th 06 01:32 AM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 0 April 27th 05 07:46 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 23 April 23rd 05 09:26 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 0 April 22nd 05 03:30 PM


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

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

About Us

"It's about Microsoft Excel"