byval and object instance

W

WG

Here is an ASP code sample in VBScript:

<%
Dim x
Set x = server.CreateObject("ADODB.RECORDSET")
x.CursorType = 1
x.CursorLocation = 3
x.Fields.Append "custtype", adVarNumeric, 80, adFldMayBeNull
x.Open
x.addnew "custtype", 34
x.Update

call dothis(x)

Response.Write(x.RecordCount)

Set x = nothing

Function dothis(byval x)

x.addnew "custtype", 34
Response.Write(x.RecordCount & "<BR>")

x.addnew "custtype", 34
Response.Write(x.RecordCount & "<BR>")

set x = nothing

End Function

%>

This should run if you put it into an asp page and pull it from IIS.
It is just a disconnected recordset filled with a record and then a
response.write, etc.

Question: I'm using "byval" in the dothis() function, but SHOULD I
set x = nothing within the function? Obviously when using, "byref",
setting x = nothing will delete the instance of the object, but with
"byval" will I leave something in memory if I don't set x = nothing?

I think this is another "age old" question but I've got some pages
acting funky on a hosted server in Canada and I think this issue may
have something to do with it.
 
B

Bob Barrows [MVP]

WG@. said:
Here is an ASP code sample in VBScript:

<%
Dim x
Set x = server.CreateObject("ADODB.RECORDSET")
x.CursorType = 1
x.CursorLocation = 3
x.Fields.Append "custtype", adVarNumeric, 80, adFldMayBeNull
x.Open
x.addnew "custtype", 34
x.Update

call dothis(x)

Response.Write(x.RecordCount)

Set x = nothing

Function dothis(byval x)

x.addnew "custtype", 34
Response.Write(x.RecordCount & "<BR>")

x.addnew "custtype", 34
Response.Write(x.RecordCount & "<BR>")

set x = nothing

End Function

%>

This should run if you put it into an asp page and pull it from IIS.
It is just a disconnected recordset

<pet peeve ignorable="true">
Actually, it's an "ad hoc" recordset. "Disconnected" implies that the
recordset was once connected to a database and disconnected after it was
opened. "Disconnected" recordsets can be reconnected to the database from
which it was disconnected. "Ad hoc" recordsets can never be connected to a
database.
filled with a record and then a
response.write, etc.

Question: I'm using "byval" in the dothis() function, but SHOULD I
set x = nothing within the function? Obviously when using, "byref",
setting x = nothing will delete the instance of the object, but with
"byval" will I leave something in memory if I don't set x = nothing?

Objects are always passed ByRef, even when you specify ByVal. You can prove
this by the following code:
<%
test

sub test
dim rs, fld
set rs=CreateObject("adodb.recordset")
rs.Fields.append "Field1",adVarChar,20
rs.Open
rs.AddNew array("Field1"),array("Test")
Response.Write "Before calling sub:<BR>"
Response.Write rs.GetString(adClipString,,"; ","<BR>","null")
AddFieldAndRecord rs
rs.MoveFirst
Response.Write "<BR>After calling sub:<BR>"
Response.Write rs.GetString(adClipString,,"; ","<BR>","null")
rs.Close:set rs=nothing
end sub

sub AddFieldAndRecord(ByVal pRs)
prs.AddNew array("Field1"),array("Test2")
prs.MoveFirst
Response.Write "<BR>Inside sub:<BR>"
Response.Write prs.GetString(adClipString,,"; ","<BR>","null")
end sub

%>
I think this is another "age old" question but I've got some pages
acting funky on a hosted server in Canada and I think this issue may
have something to do with it.

Since there is no "parent" object (such as a connection), this recordset
object should be successfully cleaned up by the garbage collector (GC).
However, you should explicitly close the recordset when finished with it,
just to be sure that the GC can successfully destroy it. Explicitly setting
a recordset to Nothing should cause it to be closed, but there is no harm in
making sure ...

More info about garbage collection can be found at:
http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx

Bob Barrows
 
W

WG

<pet peeve ignorable="true">
Actually, it's an "ad hoc" recordset. "Disconnected" implies that the
recordset was once connected to a database and disconnected after it was
opened. "Disconnected" recordsets can be reconnected to the database from
which it was disconnected. "Ad hoc" recordsets can never be connected to a
database.


Objects are always passed ByRef, even when you specify ByVal. You can prove
this by the following code:
<%
test

sub test
dim rs, fld
set rs=CreateObject("adodb.recordset")
rs.Fields.append "Field1",adVarChar,20
rs.Open
rs.AddNew array("Field1"),array("Test")
Response.Write "Before calling sub:<BR>"
Response.Write rs.GetString(adClipString,,"; ","<BR>","null")
AddFieldAndRecord rs
rs.MoveFirst
Response.Write "<BR>After calling sub:<BR>"
Response.Write rs.GetString(adClipString,,"; ","<BR>","null")
rs.Close:set rs=nothing
end sub

sub AddFieldAndRecord(ByVal pRs)
prs.AddNew array("Field1"),array("Test2")
prs.MoveFirst
Response.Write "<BR>Inside sub:<BR>"
Response.Write prs.GetString(adClipString,,"; ","<BR>","null")
end sub

%>

Change your sub to the sub below and it produces an error "Object
Required" in sub test because the instance of the object created has
been destroyed; then change ByRef to ByVal. The error goes away;
setting pRs = nothing has no effect on the instance of the object in
memory. When using ByVal, I believe all that gets passed is the
"cursor", but not a "copy" of the object (basically just like ByRef).
Notice the var type of pRs when using ByVal is a "9" "Automation
Object" (which makes sense anyway, I guess). COM is paying attention
to a difference between byval and byref.

Is it neccessary, or a "good idea" to set the locally dimensioned pRs
to nothing when using ByVal? Or, does the variable (pointer) pRs just
go out of scope?


sub AddFieldAndRecord(ByRef pRs)
prs.AddNew array("Field1"),array("Test2")
prs.MoveFirst
Response.Write "<BR>Inside sub:<BR>"
Response.Write prs.GetString(adClipString,,"; ","<BR>","null")

Response.Write "<BR>pRs vartype:<BR>"
Response.Write(varType(pRs))

set pRs = nothing

end sub
 
B

Bob Barrows [MVP]

WG@. said:
Change your sub to the sub below and it produces an error "Object
Required" in sub test because the instance of the object created has
been destroyed; then change ByRef to ByVal. The error goes away;
setting pRs = nothing has no effect on the instance of the object in
memory.

This may help.
http://blogs.msdn.com/ericlippert/archive/0001/01/01/53005.aspx
If not, I'm out of my depth. You may wish to email Eric (he's on vacation at
the moment

When using ByVal, I believe all that gets passed is the
"cursor", but not a "copy" of the object (basically just like ByRef).

This does not sound correct, but again, I'm out of my depth ...
Notice the var type of pRs when using ByVal is a "9" "Automation
Object" (which makes sense anyway, I guess). COM is paying attention
to a difference between byval and byref.

I don't understand the point you are making here. The vartype is 9
regardless of whether byval or byref is used ...
Is it neccessary, or a "good idea" to set the locally dimensioned pRs
to nothing when using ByVal? Or, does the variable (pointer) pRs just
go out of scope?

Again, I believe it is not necessary given the lack of a parent object.
 

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
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top