Good question...
First let's determine what the differences are and then talk about why you would use one or the other.
When you do this:
Dim obj As New objType
you are defining obj implicitly.
When you do this:
Dim obj as objType
Set obj = New objType
you are defining obj explicitly.
When you use New in the Dim statement, the object will be created the first time the object is refererenced. For example
Dim objCustomer as New clsCustomer
objCustomer.FirstName = "Joe"
The setting of .FirstName does two things. First it creates the object if it doesn't exist, and secondly it sets the value of FirstName to "Joe". This is implicit object creation.
When you don't use the New statement in the Dim statement you have to EXPLICITYLY create the object like this, before you can use it:
Set objCustomer = New clsCustomer
Now lets look at this:
Dim objCustomer As clsCustomer
objCustomer.FirstName = "Joe"
The above code would generate an error 91, object or with block not set. Why? Because the object is not yet created. In order to make it work correctly you have to write it like this:
Dim objCustomer as clsCustomer
Set objCustomer = New clsCustomer
objCustomer.FirstName = "Joe"
Now it works just fine. Also note that you use the same syntax to destroy the object regardless of how it's created.
Set objCustomer = Nothing
To understand where and when to use one over the other, lets look at an example:
Dim objCustomer As New clsCustomer
objCustomer.FirstName = "Joe"
Debug.Print objCustomer.FirstName
Set objCustomer = Nothing
Debug.Pring objCustomer.FirstName
Before you read on, try to determine what the output of the above program would be for both debug.print statements. This is a question that I like to ask a VB Programmer during an interview.
If you said it generated an error, you weren't correct.
If you said it would print "Joe" and "" (empty string) you were right.
Now lets determine what's really happening... We're creating the object, then using it, then destroying it. Since we destroyed it, it probably meant we were done with it. HOWEVER.. there's another reference to it after we destroyed it. So it creates a new object, with all the properties blank, but doesn't give us an error or any warning as to what it did. It is for this reason that I DON'T LIKE IMPLICIT OBJECT CREATION!!! I feel that defining the New Keyword in the variable declaration is asking for trouble. Things like this can lurk in the code and drive you crazy. Now imagine the same program as above, but take the NEW keyword out of the variable declaration. If you run the program again, you'll get an error when you reference the FirstName property after the object was destroyed. In my opinion this is much better because it's probably telling you that you did something that you didn't really mean to do. If you let the object recreate itself, it's harder to find out why all your properties are blank and 0 or different, then if you find out that you're trying to use an object that's not created yet.
I use this as a rule of thumb. When you use an implied programming style it makes the code harder to understand, especially if you're the one looking at someone else's implied programming style of code.
When I see objCustomer.FirstName and there is no object created before it, I ask myself, "Is this really what they inteded to do?". If I see Set objCustomer = New clsCustomer then there's no doubt in my mind that the programmer EXPLICITLY wanted that to be there. Same thing for DIM statements at module levels.
If I said:
Option Explicit
Dim x As Integer
What is the intended scope? You don't know cause it's implied, unless you know the scope rules of the different types of modules. If it's a Class or a Form, the scope is Private, if it's in a module, the scope is public. It would be much better to say:
Option Explicit
Private x As Integer
That way I know the programmer specified the scope of the variable.
I tend to shy away from IMPLICIT programming just because it seems to be so vague. I try to explicitly state what I want to happen in the code.
As far as
Set objCustomer = New clsCustomer
vs.
Set objCustomer = CreateObject("MYDLL.clsCustomer"
I couldn't tell you which one is faster or better. I think it's just a matter of style. I believe it's two different ways to say the same thing.
Just my $2.50 worth.... Snaggs
tribesaddict@swbell.net
Life can only be understood backwards; but it must be lived forwards.