472,354 Members | 1,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

What is the difference between ???

Hi All ,

can anyone tell me what is the difference between the following declaration
and how it affects
application performance.

1.
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

2. Dim cn As New ADODB.Connection

Nov 20 '05 #1
3 2880
These newsgroups are for the .Net languages. For VB6, you should use the
microsoft.public.vb* hierarchy.

That said, there shouldn't be any performance differences between them.
They are simply different variations of the same code.

--Robert Jacobson
"Anoj Kumar" <an************@yahoo.com> wrote in message
news:uj**************@TK2MSFTNGP10.phx.gbl...
Hi All ,

can anyone tell me what is the difference between the following declaration and how it affects
application performance.

1.
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

2. Dim cn As New ADODB.Connection


Nov 20 '05 #2
Anoj Kumar wrote:
Hi All ,

can anyone tell me what is the difference between the following declaration
and how it affects
application performance.

1.
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

2. Dim cn As New ADODB.Connection


Since your using Set, I have to assume that this is VB6 code. If that
is the case, there is a subtle difference and the first option is
usually the prefered method. The difference is mainly as to when the
intialization of the object takes place. With the As New syntax,
initialization is delayed until the first reference to the object. In
other words:

Dim o As New MyObjectType ' o is not created here

' Do Cool Stuff
.....

o.ExecuteAMethod() ' o is created and then the method called.
The main problem with this behavior is that, not only does it introduce
some very subtle bugs, but it can adversely affect performance since VB
is forced to check the integrity of the reference every time you use
it... For example if you have an object declared this way in a loop,
and then you call a whole bunch of methods/properties you end up
generating something like this around every call:

If o Is Nothing Then
Set o = New MyObject
o.ExecuteTheMethod()
Else
o.ExecuteTheMethod()
End If

Anyway, the point is that this can be an expensive operation. The bugs
come into play when you have something like this:

Dim f As New Form

' do stuff with f

Set f = Nothing

If f Is Nothing Then
MsgBox "The Form Is Nothing"
Else
MsgBox "Hey, Form Still Exists!"
End If

In the above code, the mere act of testing f for nothing causes it to be
reinstantiated - resulting in zombi-like behavior for objects crated "As
New". Basically, it is usually considered bad practice to use this form
of declaration in VB.

Now, if you were talking VB.NET and somehow just slipped in the Set -
well then ignore all of the above, since this isn't true in VB.NET.
VB.NET intializes the object at the point of the As New. The net result
is that there is no effective difference between:

Dim o As SomeObject
o = New SomeObject()

And

Dim o As New SomeObject

HTH,
Tom Shelton

Nov 20 '05 #3
Nice one Tom :-)

Regards,
Fergus
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
1
by: mike | last post by:
best regards: What is the difference between DOM Level 1 and DOM Level 2. Thank you. May god be with you.
5
by: radnus2004 | last post by:
Hi all, I am not clear what is the difference between signed and unsigned in C. Many say, unsigned means only +ve values and 0. Can I use unsigned int i = -3? What happens internally ? What...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
12
by: Nathan Sokalski | last post by:
What is the difference between the Page_Init and Page_Load events? When I was debugging my code, they both seemed to get triggered on every postback. I am assuming that there is some difference,...
6
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. What is the difference between a Field and a Property? Here is the context. In the VS.NET 2002 documentation, in the "String Members" section, we have the...
5
by: kai | last post by:
Hi, In ASP.NET , what is the difference between OnClick and Click events for a button? Because we have button click event, it can trigger events, why we still need OnClick? Please help. ...
49
by: Zach | last post by:
After having taken a looong break from VB (last used 6.0), I started getting back into programming again, this time with VS 2005. I began reading up on VB.NET from various sources (books,...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.