Forgive me!

L

Luis

A dumb friday-afternoon-and-I-need-a-weekend question.

I inherited an application that has code similar to:

set conn = server.CreateObject("ADODB.Connection")
set New_app = server.CreateObject("ADODB.Recordset")
set New_app = conn.Execute("SELECT * FROM TableName WHERE Blah = '"&
blah & "'")

The code executes a "set New_app" instruction TWO TIMES. Doesn't the
second "set New_app" instruction override the first "set New_app"
instruction?

And is it correct that the
set New_app = server.CreateObject("ADODB.Recordset")
instruction is totally unnecessary anyway?

Thanks
 
C

CJM

First of all, 'dumb' questions are inevitable sometimes, so you dont need
the 'Forgive Me'
Second, it helps the rest of us figure out what kind of dumb question you
are asking if you give a more meaningful Subject line.
Thirdly, it's not really much of a dumb question at all.

Given this example, you don't need to declare New_app as a recordset, but
you are entitled to. There are times where it is useful/necessary, e.g if
you explicitly declare a variable as a recordset, intellisense can then list
the properties and methods for you.

Yes, this runs the 'Set New_app' code twice but it is doing two different
things:
- The first line states that you want New_app to hold an empty recordset.
- The conn.Execute in the second line returns a recordset that is then
stored in New_app.

In this example you *can* miss out the first Set statement.

There are other techniques and styles in common use, in which case declare a
variable explicitly in this way is needed.

hth

Chris
 
C

Chris Barber

Looks like the original developer wanted the intellisense feature to
kick-off for the New_App object - it only happens if you do an explicit
CreateObject(ProgID) call (at least in Interdev).

In terms of code execution the speed diff will be negligible (don't ask me
to define 'negligible' please). The second call is just modifying the
pointer and letting the previous object destroy itself.

I use that construct a lot where I'm getting objects passed back from other
objects (especially for my own DLL based components) since Interdev then
helps me with my own DLL method calls. I know it's not the most efficient
but it does help to stop me from making stupid method call mistakes.

Chris.

A dumb friday-afternoon-and-I-need-a-weekend question.

I inherited an application that has code similar to:

set conn = server.CreateObject("ADODB.Connection")
set New_app = server.CreateObject("ADODB.Recordset")
set New_app = conn.Execute("SELECT * FROM TableName WHERE Blah = '"&
blah & "'")

The code executes a "set New_app" instruction TWO TIMES. Doesn't the
second "set New_app" instruction override the first "set New_app"
instruction?

And is it correct that the
set New_app = server.CreateObject("ADODB.Recordset")
instruction is totally unnecessary anyway?

Thanks
 
M

Michael G. Schneider

I use that construct a lot where I'm getting objects passed back from other
objects (especially for my own DLL based components) since Interdev then
helps me with my own DLL method calls. I know it's not the most efficient
but it does help to stop me from making stupid method call mistakes.

I do also use this feature for making Intellisense work. What I found out,
is the following: the statement with the CreateObject assignment does not
have to be executed, it does not even have to be reachable. For example my
ASP-pages always contain code like:

<%
' here is the real code

Sub DummyNeverToBeExecuted()
Dim rs
Set rs = CreateObject("ADODB.Recordset")
' and many more assignments with typical variable names
End Sub
%>

Any occurence of the variable rs in the "real code" will then offer
intellisense.

Michael G. Schneider
 
C

Chris Barber

Yeah, I also found that.

It also screws up if you have two same named object variables in different
subs (eg. diff scope) using different ProgID's!

Thats a pain in the butt when doing class based VBScript against DLL stuff.

Chris.

I use that construct a lot where I'm getting objects passed back from other
objects (especially for my own DLL based components) since Interdev then
helps me with my own DLL method calls. I know it's not the most efficient
but it does help to stop me from making stupid method call mistakes.

I do also use this feature for making Intellisense work. What I found out,
is the following: the statement with the CreateObject assignment does not
have to be executed, it does not even have to be reachable. For example my
ASP-pages always contain code like:

<%
' here is the real code

Sub DummyNeverToBeExecuted()
Dim rs
Set rs = CreateObject("ADODB.Recordset")
' and many more assignments with typical variable names
End Sub
%>

Any occurence of the variable rs in the "real code" will then offer
intellisense.

Michael G. Schneider
 
C

CJM

While I wouldn't use your approach, I guess I've borrowed the same idea:

Sometimes, if I can't remember the right properties methods, I'll use the
CreateObject line to prompt Intellisense, then remove it when I have
finished the bulk of my coding...

Chris
 
M

Michael G. Schneider

While I wouldn't use your approach, I guess I've borrowed the same idea:

Why wouldn't you use the approach? Does it have any disadvantages, I am not
aware of?

Michael G. Schneider
 
C

Chris Barber

Yikes, an argument about Interdev?
It's a pile of poo in some respects and amazing in others.

Do what you are comfortable with and find workarounds for the rest is my
motto. As long as the code looks good, is maintainable, efficient, well
documented, reusable (oh damn, I was talking about VB again).

LoL.

Chris.

While I wouldn't use your approach, I guess I've borrowed the same idea:

Why wouldn't you use the approach? Does it have any disadvantages, I am not
aware of?

Michael G. Schneider
 
C

CJM

I meant 'I wouldnt use this approach', as opposed to 'One shouldn't use this
approach'...

I don't know if there is any overhead with your solution, but if there is I
imagine it's minimal. But personally I'd prefer not to leave code in their
that doesnt do anything. Like I said b4, I put the declaration in to
stimulate intellisense, but take it out when finished.

Cheers

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,804
Members
47,350
Latest member
TamiPutnam

Latest Threads

Top