Dim as New vs. Set = New 
Author Message
 Dim as New vs. Set = New

I have been reading about the use of the new keyword. The documentation suggest to use it in the set  instead of in the dim statement because this produce a faster code.

But I'm still seeing a lot of examples that use the new keyword in the dim statement for collections.

Why ?
Is it true that the new keyword in the set statement produce faster code that in the dim ?
Is it only true for objects, not for collections ?
Or Is the diference between both options insignificant ?

Thanks in advance ...

Gabriel E. C. Coy - Buenos Aires - Argentina
Member of:
Microsoft Site Developer Network Level II
WorldWide SQL Server User's Group (www.swynk.com/sswug)



Mon, 17 Jul 2000 03:00:00 GMT  
 Dim as New vs. Set = New



I have been reading about the use of the new keyword. The documentation
suggest to use it in the set  instead of in the dim statement because this
produce a faster code.

But I'm still seeing a lot of examples that use the new keyword in the dim
statement for collections.

Why ?
Is it true that the new keyword in the set statement produce faster code
that in the dim ?
Is it only true for objects, not for collections ?
Or Is the diference between both options insignificant ?

Thanks in advance ...

Gabriel E. C. Coy - Buenos Aires - Argentina
Member of:
Microsoft Site Developer Network Level II
WorldWide SQL Server User's Group (www.swynk.com/sswug)

----------
The problem with "Dim As New" is that you are asking VB to instantiate a
new instance every time you reference it and that can hide bugs in the
code.  It also means that you can never test for "nothing":
Dim X As New Class1
Set X=Nothing
If X Is Nothing Then msgbox "This can never display!"
The very act of referencing X to test it causes a new Class1 to
instantiate.  It becomes very easy to create lots of objects
unintentionally.  The speed difference would only be that VB is checking
before use to ensure the variable points to an instance.  I'm not sure if
it checks before using or traps the error and "corrects" it automatically,
but either way you've added some code to the processing and if you use the
variable many times you are probably adding some time.  I'd have to run
some benchmarks to test that.  The lack of control over the creation and
deletion of objects is more than enough reason to avoid the "Dim As New"
construct and always create objects expicitly with "Set =New" instead.



Mon, 17 Jul 2000 03:00:00 GMT  
 Dim as New vs. Set = New

Gabriel,

When you Dim an Object Variable Using the New KeyWord, The Object is
Instantiated Immediately and automatically upon your first reference to it
in code (It does not = "Nothing"). Because of this you do not need to use
the Set keyword prior to using the variable. (it's value is already set...)

When you Do not use the Keyword New in the Dim Statement, the variable
holds no value or reference to an object yet (The variable = "Nothing").
Because of this, you must use a set command later on in code.

Now, the reason that using Set later in code provides for a faster
application is the fact that if the New Keyword is used in the dim
statement, what happens is When VB Sees a reference to the variable in
code, it looks at the dim statement and notices the new keyword.  because
it notices the new keyword, it thinks that you may be referencing the
variable for the first time.  If you are referencing it for the first time,
than VB Must instantiate the variable.  If this is not the first time that
the variable is referenced, VB Does not want to Instantiate The object
again.

The slowed response time is due to VB's Having to check and see if the
variable has been instantiated again.

Sorry that my response is so wordy, and a little hard to understand.  I
haven't had my coffee yet.....

Cheers

Greg Jackson
PDX, OR



Mon, 17 Jul 2000 03:00:00 GMT  
 Dim as New vs. Set = New

It's not only faster (because VB doesn't have to check whether it needs to
instantiate the object every time you reference a property), it's also much
safer and less confusing.  

For example, the App Wizard will create MDI child forms for you with

Dim frm1 As New Form1

If you try to refer to a property of Form1 in your code (instead of frm1),
you'll create another new instance!

I generally avoid Dim ... As New ... entirely.

Jim Deutch
MS Dev MVP



I have been reading about the use of the new keyword. The documentation
suggest to use it in the set  instead of in the dim statement because this
produce a faster code.

But I'm still seeing a lot of examples that use the new keyword in the dim
statement for collections.

Why ?
Is it true that the new keyword in the set statement produce faster code
that in the dim ?
Is it only true for objects, not for collections ?
Or Is the diference between both options insignificant ?

Thanks in advance ...

Gabriel E. C. Coy - Buenos Aires - Argentina
Member of:
Microsoft Site Developer Network Level II
WorldWide SQL Server User's Group (www.swynk.com/sswug)

----------



Mon, 17 Jul 2000 03:00:00 GMT  
 Dim as New vs. Set = New

So are you saying that the code

        Dim MyObj as object
        Set MyObj =New clsObject

is faster and generally preferred?

Can I also do:

        Dim MyObj as object
        Set MyObj = CreateObject("clsObject")

Are these the same?

    I have been reading about the use of the new keyword. The documentation suggest to use it in the set  instead of in the dim statement because this produce a faster code.

    But I'm still seeing a lot of examples that use the new keyword in the dim statement for collections.

    Why ?
    Is it true that the new keyword in the set statement produce faster code that in the dim ?
    Is it only true for objects, not for collections ?
    Or Is the diference between both options insignificant ?

    Thanks in advance ...

    Gabriel E. C. Coy - Buenos Aires - Argentina
    Member of:
    Microsoft Site Developer Network Level II
    WorldWide SQL Server User's Group (www.swynk.com/sswug)



Tue, 18 Jul 2000 03:00:00 GMT  
 Dim as New vs. Set = New

Quote:

> So are you saying that the code

>         Dim MyObj as object
>         Set MyObj =New clsObject

> is faster and generally preferred?

> Can I also do:

>         Dim MyObj as object
>         Set MyObj = CreateObject("clsObject")

> Are these the same?


>      I have been reading about the use of the new keyword. The
>      documentation suggest to use it in the set  instead of in
>      the dim statement because this produce a faster code.

>      But I'm still seeing a lot of examples that use the new
>      keyword in the dim statement for collections.

>      Why ?
>      Is it true that the new keyword in the set statement produce
>      faster code that in the dim ?
>      Is it only true for objects, not for collections ?
>      Or Is the diference between both options insignificant ?

>      Thanks in advance ...

>      Gabriel E. C. Coy - Buenos Aires - Argentina
>      Member of:
>      Microsoft Site Developer Network Level II
>      WorldWide SQL Server User's Group (www.swynk.com/sswug)

Yes, this would be the preferred (early bound) method.  Your second
example will also work but is a late bound creation (useful in certain
circumstances).

-Dave



Tue, 18 Jul 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Dim obj As New Class crt Dim Obj As Class = New Class

2. redundant New: Dim rs As New ADODB.Recordset

3. Dim, Set, New keywords

4. Instantiating classes, new vs set

5. New in declaration versus new in set statement

6. Any difference between DIM....as NEW and DIM....as....= NEW.....?

7. kyduke(kyduke@yahoo.com)'s New Logic - New sorting, New paging method

8. New New New

9. NEW NEW NEW Project...

10. Dim dsData as New Dataset

11. Dim as new

12. Dim objClass() as new cClassType --- How will this array get populated

 

 
Powered by phpBB® Forum Software