View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
deltaquattro deltaquattro is offline
external usenet poster
 
Posts: 65
Default Is it necessary to Set a Object to nothing before referencing itagain to some other object?

Hi all,

a very important question for me: suppose I am reading some objects
into a collection. Each time I create an object and add it: since I
use always the same name for the object, do I need to set the object
to Nothing before reinstancing it or not? An example follows:

(Class1)
Public InstanceName As String

(Module1)
Option Explicit

Sub AddToCollection()
Dim MyCollection As Collection
Dim Inst As Class1, x As Class1
Dim Msg As String, TheName As String

Set MyCollection = New Collection
Do
Set Inst = New Class1 ' Create a new instance of Class1.
Msg = "Please insert a new object name " & _
"or press Cancel to exit"
TheName = InputBox(Msg, "Name the Collection Items")
Inst.InstanceName = TheName ' Put name in object instance.
' If user entered name, add it to the collection.
If Inst.InstanceName < "" Then
' Add the named object to the collection.
MyCollection.Add Item:=Inst
End If
' Clear the current reference in preparation for next one.
Set Inst = Nothing
Loop Until TheName = ""

End Sub

If I omit the line

Set Inst = Nothing

the code works all the same. Is it correct to omit it? Or am I wasting
memory, and this could be dangerous in a bigger code? Thanks,

Best Regards

Sergio Rossi