473,511 Members | 16,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

byval and object instance

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.
Aug 20 '05 #1
3 1626
WG@. wrote:
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.
</pet peeve>
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/ar...28/122259.aspx

Bob Barrows
--
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"
Aug 20 '05 #2
WG
On Sat, 20 Aug 2005 11:25:56 -0400, "Bob Barrows [MVP]"
<re******@NOyahoo.SPAMcom> wrote:
WG@. wrote:
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.
</pet peeve>
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

%>


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

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/ar...28/122259.aspx

Bob Barrows


Aug 20 '05 #3
WG@. wrote:

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/ar.../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.

--
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"
Aug 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1416
by: Goncalo | last post by:
Hi. Can you tell me what's the difference between ByVal and ByRef in VB.Net. I'm asking this 'cause i'm passing an object into a function ByVal, then I change that object and when the...
16
1766
by: Richard | last post by:
Hi, I am passing a structure to a subroutine where the passed parameter has been declared as ByVal. However, changes made to the passed variable inside the subroutine flow through to the...
11
1519
by: Dmitry | last post by:
Hi there, Just came across this problem and was wondering if someone can shed a light on it as it somewhat puzzles me. Suppose I have the following classes: Public Class CTest Private...
14
1510
by: Niklas | last post by:
Hi What I have learned is that a variable is just a reference when dealing with Objects. Are you supposed to use ByVal or ByRef in functions? They produce the same result or have I missed...
2
10259
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net...
11
8095
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
7
2885
by: Monty | last post by:
Silly question: If I return an object from a property, is it returned ByRef or ByVal? Is there a way to specify one way or the other? For instance, will the code that calls the MyObject() property...
5
2086
by: Rob Meade | last post by:
Hi all, Until just recently most of my functions would have all been using Byval, when I realised the advantages of ByRef, where appropriate I have used it instead, my question - are there any...
2
1537
by: casManG | last post by:
I am working on a small project that uses the treeview control in .net 2003. I have a tree view that I am sending to a sub in order to iterate through the nodes. Public Sub test (ByVal...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.