View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips Bob Phillips is offline
external usenet poster
 
Posts: 10,593
Default Scripting.Dictionary question

When you auto-instance a variable in this way, it is created the first time
that it is referenced, but that is WHENEVER it is referenced. When it is
encountered in code, it is checked to see if it is Nothing, and if so, an
instance of the object is created.

Thus, more code is generated and executed each time the variable is
encountered. Logically, the following train of events takes place
If myObject Is Nothing Then
Set myObject = New object_type
End If

This is no big deal in itself, but this actually precludes you from testing
for Nothing in your code.

Try this, create a class (no code) called clsTest, and run this code

Dim myObject As New clsTest

If myObject Is Nothing Then
MsgBox "empty"
End If

and this code doesn't error as it should

Set myObject = Nothing
myObject.myProp = "xyz"


In short, you lose control, and that is not good.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Leo Heuser" wrote in message
...
"Bob Phillips" skrev i en meddelelse
...
Also it is bad practice to declare an object as New, it may get created
when
you least expect it, far better to us

Dim mares As Scripting.Dictionary

Set mares = New Scripting.Dictionary


--
HTH

Bob Phillips



Hi Bob

I don't understand your comment. If declared as New,
the object is *always* created the first time the
variable is used (as expected!?).
When is this bad?

Leo