Use NEW Keyword when declaring objects?

alreadyused

Well-known member
Joined
Jan 13, 2006
Messages
70
I found this article on MySQL written by Mike Hillyer;
http://dev.mysql.com/tech-resources/articles/vb-blob-handling.html

Under the topic "Connection Object" Mike states:
Mike Hillyer said:
Connection Object

Now that we have a connection string, we can connect to the database, first I will give a sample of how I connect.

Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.ConnectionString = GloConnectionString
conn.CursorLocation = adUseClient
conn.Open

In the first line we create an ADODB connection object in memory and in the second line we instanciate it. The alternative syntax to this is to type Dim conn As New ADODB.Connection

While I previously recommended the as New syntax, I have since learned that it slows down your code since it must check if the object is instanciated every time a reference is made to the object.

This article is discussing VB6; I interperet this to mean ALL objects instanciated in the declaration will feel this performance hit.

But Im curious, does anyone know if this has been fixed in VB.Net, and if so, is it all versions or only after certain years, such as fixed in 2003 but not 2002?
 
Last edited by a moderator:
Yes, thankfully this is fixed in VB.NET
Really dumb design in the first place, but then again that "performance hit" is pretty inconsequential.

The lines:
Code:
Dim o As New c
Dim o As c = New c
are the same.
 
thanks for the quick reply; so with your example, would the following now be equal to that as well (except for the extra line?)

Code:
dim o as c
o = new c(args)
 
Back
Top