473,889 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Dim cn As ADODB.Connectio n
Set cn = New ADODB.Connectio n

2. Dim cn As New ADODB.Connectio n

Nov 20 '05 #1
3 3026
These newsgroups are for the .Net languages. For VB6, you should use the
microsoft.publi c.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******** ******@TK2MSFTN GP10.phx.gbl...
Hi All ,

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

Dim cn As ADODB.Connectio n
Set cn = New ADODB.Connectio n

2. Dim cn As New ADODB.Connectio n

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.

Dim cn As ADODB.Connectio n
Set cn = New ADODB.Connectio n

2. Dim cn As New ADODB.Connectio n

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.ExecuteAMetho d() ' 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.ExecuteTheMet hod()
o.ExecuteTheMet hod()
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"
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()


Dim o As New SomeObject

Tom Shelton

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

Nov 20 '05 #4

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

Similar topics

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 software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
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.
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 conversion happens? Also on 32 and 64bit machines what happens? Can anybody explain?
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 standard doesn't even care what a normal char is (because signed and unsigned have equal behavior). For example if someone does this: unsigned char a = -2; /* or = 254 */
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
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, and I would like to know what it is so that I can take advantage of it in my code. Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
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 following...
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. Thanks kai
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, internet, etc.) and found out about the CLR and how all of the .NET languages access it, the major difference being the syntax and structure of the individual languages. What I'm wondering, since VB.NET is obviously easier to learn/use than C#.NET and...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.