closing objects after returning value in function ??

D

dotnettester

Hi,

I am maintaining some code and I see in some functions..

Function myFunction

set someobj = Server.CreateObject("MYOBJ")
.....
....
set myFunction = "somedataToReturn"
set someobj = nothing

End Function
 
D

dotnettester

sorry, my question is, is it valid to close an object after the return
statement and would it work?
 
B

Bob Barrows [MVP]

I believe both Dave and I answered the question. Nothing after the End
statement is executed.
 
B

Bob Barrows [MVP]

Oops. Ignore my previous. I thought we were still on the Response.End
statement.

There is no "return" statement in vbscript. All statements in a procedure
will be executed until the statement that ends the procedure is executed,
unless an Exit <function|sub> statement is encountered.

So in this example "set someobj = nothing" will be executed. It's easy
enough to test this:

<%
dim globalvar
globalvar="initial value"
function changevar()
changevar = "I ran"
globalvar="new value"
end function
response.write changevar()
response.write "<BR>" & globalvar
%>
 
C

Chris Hohmann

Bob Barrows said:
I believe both Dave and I answered the question. Nothing after the End
statement is executed.

Actually, this question is slightly different from the Response.End
question. In this case, dotnettester is asking whether the statements after
a function return value assignment are processed. Specifically, does
"someobj" get deallocated in the above example? The answer is yes. Here's a
proof of concept demonstrating the behavior:

<%
Option Explicit
Function Foo(bar)
Foo = 1
Response.Write "Hello World!"
End Function
Response.Write "<br>Foo(0):" & Foo(0)
%>
 
C

Chris Hohmann

Bob Barrows said:
Oops. Ignore my previous. I thought we were still on the Response.End
statement.

There is no "return" statement in vbscript. All statements in a procedure
will be executed until the statement that ends the procedure is executed,
unless an Exit <function|sub> statement is encountered.

So in this example "set someobj = nothing" will be executed. It's easy
enough to test this:
[snip]

To add to what Bob said, while there is no return statement in VBScript,
there is in JScript. It is also worth noting that the return statement in
JScript behaves as you would expect. That is, it exits the function
immediately. Therefore, the JScript counterpart to these examples would NOT
execute the statements following the return value assignment. The template
for JScript would look like this:

function foo(bar){
var retVal;
var myobj;
myobj = new ActiveXObject("My.Object");
retval = myobj.property;
myobj = null;
return retVal;
}
 

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,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top