Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Visual Basic (Microsoft) Versions 5/6 FAQ

VB Programming Concepts

Why not to use the "Dim myObj as New Object" construct by BobRodes
Posted: 1 Aug 05 (Edited 6 Aug 06)

In Java, C#, and VB.Net, using the New keyword on the same line as the declaration statement actually causes the object to be instantiated.  Not so with VB6.  In VB6, the As New syntax creates an "auto-instancing" object variable.  In other words, the code line


Dim myObj as New myClass
requires the VB compiler, every time myObj is referenced, to check and see if myObj is Nothing and if so to create an instance of myClass.  So, the code


Dim myObj as New myClass
myObj.Prop1 = "SomeValue"
myObj.Prop2 = "SomeOtherValue"
requires the compiler to check four times whether an object has been instantiated.  This represents unnecessary code overhead.

Writing the code in this way


Dim myObj as myClass
Set myObj = New myClass
myObj.Prop1 = "SomeValue"
myObj.Prop2 = "SomeOtherValue"
removes this overhead.

While there are some circumstances in which the use of the former syntax is preferable to the latter, they are few and far between.  For example, if you had a large number of instances of another object that only in certain circumstances ever used this object, and when they did only called one method of the object one time, it might make sense not to instantiate the object unless the code referenced it to avoid unnecessary memory overhead.

However, the time that you will see this most often is when the object is only referenced once, especially in conjuction with the With keyword.  For example:


Dim myObject as New myClass
With myObject
   .ThisProperty = SomeValue
   .ThatProperty = SomeOtherValue
   .AndSoOn = SomethingElse
End With
This will only actually instantiate the object once, at the time it is encountered in the with statement.  Of course, you don't really need to use a myObject variable in this case; it's more a matter of style.  You can also do it this way:


With New myClass
   .ThisProperty = SomeValue
   .ThatProperty = SomeOtherValue
   .AndSoOn = SomethingElse
End With
In effect, this is also an auto-instancing variable, since an instantiated class reference is implied (and created) by the "with new" statement.  This is actually even more efficient, as it isn't storing a variable definition throughout the life of the procedure in which it's declared.


Back to Visual Basic (Microsoft) Versions 5/6 FAQ Index
Back to Visual Basic (Microsoft) Versions 5/6 Forum

My Archive


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close