20 Ton Squirrel
New Member
- Joined
- Aug 18, 2011
- Messages
- 11
Hello all! This is a "best practice" sort of question for you fellow programmers out there.
Is it necessary to always set object variables to Nothing at the end of a procedure?
In a procedure where any sort of object variable is declared, I always make a habit of setting the object to nothing at the end of the procedure. An example:
I do this to make certain that the memory is being released for that particular variable. Doesn't matter if it is a range, worksheet, collection, or some wild class object... I always set it to Nothing.
Am I being OCD or is this a valid concern?
Is it necessary to always set object variables to Nothing at the end of a procedure?
In a procedure where any sort of object variable is declared, I always make a habit of setting the object to nothing at the end of the procedure. An example:
Code:
Public Sub sampleProcedure()
Dim wks As Worksheet
Dim rng As Range
Dim dct As Scripting.Dictionary
Set wks = ThisWorkbook.Worksheets("Sheet1")
Set rng = wks.Range("A1")
set dct = New Scripting.Dictionary
'«BRILLIANT AUTOMATION PROGRAMMING HERE»
HANDLER_EXIT:
Set wks = Nothing
Set rng = Nothing
set dct = Nothing
Exit Sub
HANDLER_ERROR_SOMETHING1:
'«BRILLIANT ERROR HANDLING HERE»
HANDLER_ERROR_SOMETHING2:
'«BRILLIANT ERROR HANDLING HERE»
End Sub
I do this to make certain that the memory is being released for that particular variable. Doesn't matter if it is a range, worksheet, collection, or some wild class object... I always set it to Nothing.
Am I being OCD or is this a valid concern?