Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Visual Basic(Microsoft): Version 5 & 6 FAQ

VB Programming Concepts

Why not to use the "Dim myObj as New Object" construct
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

CODE

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

CODE

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

Writing the code in this way

CODE

Dim myObj as myClass
Set myObj = New myClass
myObj.Prop1 = "SomeValue"
myObj.Prop2 = "SomeOtherValue"
myObj.MyMethod1
myObj.MyMethod2
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:

CODE

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:

CODE

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.

Bob

Back to Visual Basic(Microsoft): Version 5 & 6 FAQ Index
Back to Visual Basic(Microsoft): Version 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