Groups | Blog | Home
all groups > inetserver asp general > september 2005 >

inetserver asp general : closing objects after returning value in function ??


dotnettester
9/12/2005 1:01:01 PM
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
dotnettester
9/12/2005 1:04:06 PM
sorry, my question is, is it valid to close an object after the return
statement and would it work?

[quoted text, click to view]
Chris Hohmann
9/12/2005 4:24:19 PM
[quoted text, click to view]

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)
%>

Chris Hohmann
9/12/2005 4:41:44 PM
[quoted text, click to view]
[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;
}

Bob Barrows [MVP]
9/12/2005 6:45:15 PM
I believe both Dave and I answered the question. Nothing after the End
statement is executed.

[quoted text, click to view]

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Bob Barrows [MVP]
9/12/2005 6:50:29 PM
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
%>

[quoted text, click to view]

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Dave Anderson
9/13/2005 12:04:43 AM
[quoted text, click to view]

As noted elsewhere, there is no return statement in VBScript. I would also
add that the Set Statement is reserved for assigning object references, not
for assigning values.



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.

dotnettester
9/13/2005 6:38:01 AM
Thanks all.

[quoted text, click to view]
AddThis Social Bookmark Button