confusion re set = nothing

G

Gary Hillerson

I've gotten myself a bit confused about object vars.

In the following function, do I need these statements at the end to
deallocate memory that was allocated by the Set statements?

Set pgf = Nothing
Set rng = Nohting


Public Function IsFrontMatterSection(sect As Section) As Boolean
Dim pgf As Paragraph
Dim rng As Range

Set pgf = sect.Range.Paragraphs.First
Set rng = pgf.Range
IsFrontMatterSection = (rng.Style = "FrontMatter Title") _
Or (rng.Style = "Section Header")
Set pgf = Nothing
Set rng = Nohting
End Function
 
J

Jay Freedman

I'll go with Andy Pope's advice in this old thread:
http://www.ozgrid.com/forum/showthread.php?t=61298&page=1

In the example you showed, the object variables are declared within the
function (and, by default, they're Private), so their scope is just that
function. When execution hits the End Function statement, the function and
everything in it is automatically discarded, so it isn't necessary to set the
objects to Nothing.

If you had declared the objects at the module level or project level, or if
you had declared them as static, then you *should* set them to Nothing when
they're no longer needed.
 
G

Gary Hillerson

Thanks, Jay. That's always been my thinking, but at some point i
thought about it too much and started wondering if I had to have a
"set obj=nothing" for every "set obj=something".

g
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top