Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Clarification needed about Scope Of Variables

Hi,

I always thought that Variables Live till the end of the Sub / Function
and get deferenced at the end of the procedure.

But I saw this tip on a website, that within a sub, dereferences a
locally created variable to Nothing.

I'm now in doubt if I should be doing that in my procedures, since I
have some heavy variables too

And any suggestion for maintaining global constants in VBA, I have
created these strings that wont die till the Application closes

Thanks,
Megha

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Clarification needed about Scope Of Variables

The life time of variables is the scope of the procedure (sub/function) that
they were declared in or the module or project depending on the declaration
for global variables.

For object variables like worksheet, workbook, range etc (not the standard
variables like integer, long, string etc.) it is best to dereference the
variable after you have finished using it as in:

dim ws as worksheet

set ws = activesheet
ws.range("A1") = "A"
set ws = nothing

Excel has some problems with leaving the references active.

HTHs.

"vmegha" wrote:

Hi,

I always thought that Variables Live till the end of the Sub / Function
and get deferenced at the end of the procedure.

But I saw this tip on a website, that within a sub, dereferences a
locally created variable to Nothing.

I'm now in doubt if I should be doing that in my procedures, since I
have some heavy variables too

And any suggestion for maintaining global constants in VBA, I have
created these strings that wont die till the Application closes

Thanks,
Megha


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Clarification needed about Scope Of Variables

This is a debatable topic as I have mentioned before. Matt Curland, an MS
developer has some interesting things to say about it at
http://tinyurl.com/cq95p. However, there are cases, automation comes to
mind, where not clearing down the objects is suspected of being the reason
why Excel sometimes fails to quit.


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"Martin Fishlock" wrote in
message ...
The life time of variables is the scope of the procedure (sub/function)

that
they were declared in or the module or project depending on the

declaration
for global variables.

For object variables like worksheet, workbook, range etc (not the standard
variables like integer, long, string etc.) it is best to dereference the
variable after you have finished using it as in:

dim ws as worksheet

set ws = activesheet
ws.range("A1") = "A"
set ws = nothing

Excel has some problems with leaving the references active.

HTHs.

"vmegha" wrote:

Hi,

I always thought that Variables Live till the end of the Sub / Function
and get deferenced at the end of the procedure.

But I saw this tip on a website, that within a sub, dereferences a
locally created variable to Nothing.

I'm now in doubt if I should be doing that in my procedures, since I
have some heavy variables too

And any suggestion for maintaining global constants in VBA, I have
created these strings that wont die till the Application closes

Thanks,
Megha




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Clarification needed about Scope Of Variables

Variables do live till the end of a sub/function, that is true. But if
you assign an object with the set statement it is a bit a different
situation. Every object has a reference counter and bascially counts
how many variables reference this object. When you use the set
statement, it will increase the Ref counter and when you use Nothing,
it will decrease the Ref counter. When the Ref counter is Zero, the
object will be cleaned up. So, yes, set every variable to Nothing, if
you assigned an object to it via the "Set" command.

Global string constants, I think that is ok, have a few of them myself
:-)

Cheers

Remy Blaettler
http://www.collaboral.com

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Clarification needed about Scope Of Variables

Thanks all, I guess it's just safer to dereference the objects... I've
created.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Clarification needed about Scope Of Variables

Well it certainly doesn't hurt.

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"vmegha" wrote in message
oups.com...
Thanks all, I guess it's just safer to dereference the objects... I've
created.



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Clarification needed about Scope Of Variables

Well it certainly doesn't hurt.

Contradicting your own post?

Setting them to Nothing
yourself is a waste of code: VB has to check if they are Nothing anyway and
always generates the code the code to release them, so you're just
duplicating existing code.


Also, unneeded teardown code
obscures necessary teardown code buy burying it in a bunch of noise, which
makes it harder to maintain the real teardown code.


As to Remy's comment, has he ever written a strictly Excel internal vba
procedure that won't let Excel close because an object reference is
retained. I grant that his comment is correct for C++, but in VBA, VBA
handles that for the coder. If referencing automation, then the problem
with automation is not with Set statements, but with the creation of
implicit references that can not be released.

--
Regards,
Tom Ogilvy




"Bob Phillips" wrote in message
...


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"vmegha" wrote in message
oups.com...
Thanks all, I guess it's just safer to dereference the objects... I've
created.





  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Clarification needed about Scope Of Variables

Life and learn...
But yeah, too much C++ background. Glad C# is here :-)

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
Scope of Variables leerem Excel Discussion (Misc queries) 4 September 30th 08 12:54 PM
Public variables and scope Dennis Benjamin Excel Programming 3 December 2nd 05 04:41 AM
VERY Basic Define Variables Help Needed BEEJAY Excel Programming 12 September 30th 05 08:10 PM
Scope of variables Purnima Excel Programming 1 April 5th 05 05:59 AM
clarification Michael Joe Excel Programming 3 August 13th 04 09:49 PM


All times are GMT +1. The time now is 12:39 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"