Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Dim MyObject As New ... vs. Set MyObject = New ...
Message
 
À
26/08/1999 17:29:12
Information générale
Forum:
Visual Basic
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00258165
Message ID:
00258231
Vues:
12
>Can anyone tell me what difference there is (other than 1 line of code) between using the New keyword in a Dim statement, or in a Set statement?

I will try to serve you my best.

This is by far more a performance issue than only a matter of a line of code issue.

The compiler cannot create the instance of your object while it is dimming your object.

This syntax is the most efficient:
Dim MyObject2 As MyClass()
Set MyObject2 = New MyClass

The reason is, that if you use the following syntax:
Dim MyObject1 As New MyClass()
MyObject1.MyValue = "Some Value"
MyObject1.MyValue2 = "Some Other Value"

the compiler will change to something your code to something like:
Dim MyObject1 As New MyClass()

If MyObject1 Is Nothing Then
Set MyObject1 = New MyObject1
End If
MyObject1.MyValue = "Some Value"

If MyObject1 Is Nothing Then
Set MyObject1 = New MyObject1
End If
MyObject1.MyValue2 = "Some Other Value"

Every time the compiler will see MyObject1, it has to check if it has to instantiates it.

I hope you understand what I wanted to say. If not, come back here!
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform