The New Keyword

Dim clsOne As Class1
Dim clsTwo As New Class1

Those are two ways to declare a variable as a class. The first way simply data types the variable; always a good coding practice. The second way includes the New keyword which data types the variable and directs the compiler to instantiate the class whenever it’s used.

Without New you would, later in your code, use a statement like

Set clsOne = New Class1

to instantiate the class manually. With New, VBA instantiates the class whenever you need it. That may be convenient, but it adds unnecessary code. As Chip Pearson put it

So when you have code like

Dim X As New Class1
X.Property = 123

the compiler actually creates the code

Dim X As Class1
If X Is Nothing Then
Set X = New Class1
End If
X.Property = 123

If you’re only referencing X’s property once, then you’ve lost nothing by using New in the Dim statement. However, that extra bit of code the compiler adds is added to every line that references that class. The compiler has to check every time. It’s far more efficient for you to check when appropriate.

Based on Chip’s explanation, I can’ t think of any reason to ever use the New keyword in a Dim statement. (To be sure, I’ve been guilty of it, but I always try to control when the class is created rather than let VBA do it. ) If you put that If block before every class reference but one, you’d be better off than using New, if only a microsecond better.

The rule of the day is: Never use New in a Dim statement. If your code has multiple places where a class can be created, include the test before the first use in each of those places. Once you’ve referenced the class, you shouldn’t need to test it anymore.

Posted in Uncategorized

4 thoughts on “The New Keyword

  1. Excellent Article! This is one of the best explanations I’ve seen on the subject. It really bugs me that about 95% of all code I see that deals w/classes uses New in the Dim statement! Spread the word!

    p.s. Great site – I can’t believe that I haven’t heard of it before, but I’ll be sure to link to it once I update my site.

  2. While machine benefits are nice, they are never the primary reason why I make conceptual decisions. However, there is a important *functional* reason to not use New in the Dim statement. It becomes impossible to test if an object is nothing :( Try the following:

    Sub testIt()
        Dim x As New Class1
        Set x = Nothing
        MsgBox x Is Nothing
        End Sub

    The message box display will always be false. Get rid of the New in the Dim statement and it becomes possible to test for nothing.

    That said, in VB.Net, it is strongly recommended to use the New clause in the Dim statement. Apparently, the overhead no longer exists. Don’t ask me how or why.

  3. Dick, the post itself is wonderful and Mr. Tushar has added a more realistic proof for avoidance of usage of New keyword. I tried the code and it did return me false. To conclude, I am in agreement with this post and gained some good knowledge from here. Thanks guys. Cheers !

Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.