Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Dim object as new" And "set object = new" 1

Status
Not open for further replies.

SH4F33

Programmer
Apr 1, 2005
58
MU
Code:
Dim objRS as New Recordset

Code:
Dim objRS as Recordset
Set objRs = New Recordset

Both of the codes above will create a new recordset,but I still dont know wats the difference between them. Ive always used the first one, but I'd like to know how they differ.
 
Have a look at thread222-297780. I know there is also a FAQ and also another large thread that discussed this top (which I can not find at the moment).
 
The 2nd one is better because you have explicit control over when the variable is instantiated. There are situations where VB (usually during error resuming conditions) will attempt to instantiate variables for you, and you'll see strange behavior. By writing your code to do this in the 2nd style, you'll know exactly when your variables are getting created.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Often the "problems" with As New take the form:

[tt]Dim objA As New Lib.A

objA.Method 'Automagically creates an instance of Lib.A
Set objA = Nothing
objA.Method 'Automagically creates a 2nd instance[/tt]

The whole thing can be even more complicated. Sometimes:

[tt]Set objA = New Lib.A
Set objA = CreateObject("Lib.A")[/tt]

... can produce different results, even if both cases use early binding. See discussions such as:

CreateObject vs. New

In a non-MTS scenario this is most important for things like ActiveX EXEs that may create instances of single-use classes that they export themselves! I found this handy when creating a custom multi-user remote shell where each client connection needed an isolated process on the server.

But for most general use one can treat the two as identical in function.
 
Sorry 4 the late response dilettante, thanks for the link.
 
faq222-6008 gives a little more material for the OP's question. As for dilettante's post, one more thing is that using New in an MTS context fails to create the ContextObject, which is the point of MTS in the first place. It's not considered good practice to use the New keyword in objects that are intended for MTS use.

Finally, the thread that bj references asks if it's the same way in .Net. Just for the record, it isn't, although that's pretty much common knowledge by now.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top