Byref

L

Logico

Hi everybody, I've tried to use the byref keyword for passing
arguments to subroutines and functions in my ASP pages with VBScript,
but it seems that both byref and byval are irrilevant, as simple
variables and arrays are always passed by value and objects (I tried
dictionary ones) are always passed by reference. Is it correct? Or I'm
wrong and I did a mistake on my test code (that you can find below)?
Can someone explain to me why there is this behaviour? Then, where is
the "byref" used? Thanks,

Alessio

<%
Sub square(byval num)
num=num*num
end Sub
Sub incrementa(byref num)
num=num+100
end Sub

Sub squareA(byval a)
dim k
for k=lbound(a) to ubound(a)
a(k) = a(k) * a(k)
next
end Sub
Sub incrementaA(byref a)
dim k
for k=lbound(a) to ubound(a)
a(k) = a(k) + 10
next
end Sub

Sub squareD(byval d)
dim k
for each k in d
d.item(k) = d.item(k) * d.item(k)
next
end Sub
Sub incrementaD(byref d)
dim k
for each k in d
d.item(k) = d.item(k) + 10
next
end Sub


dim b
b=5
Response.Write("b=" & b & "#<br>")
square(b)
Response.Write("b=" & b & "#<br>")
incrementa(b)
Response.Write("b=" & b & "#<br>")


dim arr
arr=Array(5, 7, 10)
Response.Write("arr(1)=" & arr(1) & "#<br>")
squareA(arr)
Response.Write("arr(1)=" & arr(1) & "#<br>")
incrementaA(arr)
Response.Write("arr(1)=" & arr(1) & "#<br>")


dim dict, i
set dict=server.CreateObject("Scripting.Dictionary")
for i=1 to 3
dict.Add "K" & i, i
next
Response.Write("dict.item('K2')=" & dict.item("K2") & "#<br>")
squareD(dict)
Response.Write("dict.item('K2')=" & dict.item("K2") & "#<br>")
incrementaD(dict)
Response.Write("dict.item('K2')=" & dict.item("K2") & "#<br>")
set dict=nothing
%>
 
M

Mark Schupp

IIRC the default is ByRef. I normally specify byref or byval explicitly both
to be sure and as a documentation aid. Objects and arrays may ignore appear
to always be passed ByRef because the variable is a reference to the object.
I haven't tried to experiment with this but you could try passing an object
byval and set the parameter to another object inside the sub. Then see if it
changes.

You did not include the output of your test script so there is no way to see
if it behaves as expected. Try something simple first like:

sub byvalue( byval n )
n = 2
end sub

sub byreference( byref n )
n = 3
end sub

dim n
n = 1
Response.write "n=" & CStr(n) & "<br>"

call byvalue(n)
Response.write "n=" & CStr(n) & "<br>"

call byreference(n)
Response.write "n=" & CStr(n) & "<br>"

You should get:
n=1
n=1
n=3
 
B

Bob Barrows [MVP]

Logico said:
Hi everybody, I've tried to use the byref keyword for passing
arguments to subroutines and functions in my ASP pages with VBScript,
but it seems that both byref and byval are irrilevant, as simple
variables and arrays are always passed by value and objects (I tried
dictionary ones) are always passed by reference. Is it correct? Or I'm
wrong and I did a mistake on my test code (that you can find below)?
Can someone explain to me why there is this behaviour? Then, where is
the "byref" used? Thanks,

squareD(b)

Using parentheses when passing an argument forces the argument to be passed
byval (simplified explanation). Do not use parentheses when calling a sub:

squareD b

For more information, see here:

http://blogs.msdn.com/ericlippert/archive/2003/09/15/52996.aspx

Bob Barrows
 
S

StephenMcC

Hey,

I have a another query on this issue, when using ByRef parameters,
espiceally in ASP with VBScript, is it more expensive to use ByRef than ByVal
params...ie: as below...

sOutPut = MyFunction(Data1, Data2)

Public Function MyFunction(ByRef Param1, ByRef Param2)
some code
...
End Sub

....if the Param1 & Param2 are not going to be changed inside the function
call then should I drop the use of ByRef in the function definition and pass
the params ByVal: 'sOutPut = MyFunction((Data1), (Data2))' , might this help
save memory/resources, espiceally if this call is used many times in a single
page?

Any insight into this would be most helpful, any links even better!

Cheers.

StephenMcC
..
 
E

Evertjan.

=?Utf-8?B?U3RlcGhlbk1jQw==?= wrote on 09 mei 2005 in
microsoft.public.inetserver.asp.general:
I have a another query on this issue, when using ByRef parameters,
espiceally in ASP with VBScript, is it more expensive to use ByRef
than ByVal params...ie: as below...

sOutPut = MyFunction(Data1, Data2)

Public Function MyFunction(ByRef Param1, ByRef Param2)
some code
...
End Sub

Very expensive.
As this is not part of vbscript under ASP,
you will be fined with an error.
 
S

StephenMcC

Hi Evertjan,

Thanks for ur speedy reply. Sorry but I'm at a loss, do u mean the function
declaration will cause an error !? This is how we are currently doing it and
it doesn't complain. I think I may change the code to pass ByVal where no
return is required, in the call to the function droping parentheses around
the params not to be returned.

StephenMcC
..
 
B

Bob Barrows [MVP]

Evertjan. said:
=?Utf-8?B?U3RlcGhlbk1jQw==?= wrote on 09 mei 2005 in
microsoft.public.inetserver.asp.general:


Very expensive.
As this is not part of vbscript under ASP,
you will be fined with an error.
?
Are you talking about "End Sub" instead of "End Function"?
 
B

Bob Barrows [MVP]

StephenMcC said:
Hey,

I have a another query on this issue, when using ByRef parameters,
espiceally in ASP with VBScript, is it more expensive to use ByRef
than ByVal params...ie: as below...

Since a copy of the variable is made, yes ByVal is a little more expensive.
However, I sincerely doubt that anyone would ever notice the difference. I
tend to use ByVal unless ByRef functionality s needed.
Bob Barrows
 
M

Mark J. McGinty

StephenMcC said:
Hey,

I have a another query on this issue, when using ByRef parameters,
espiceally in ASP with VBScript, is it more expensive to use ByRef than
ByVal
params...ie: as below...

sOutPut = MyFunction(Data1, Data2)

Public Function MyFunction(ByRef Param1, ByRef Param2)
some code
...
End Sub

...if the Param1 & Param2 are not going to be changed inside the function
call then should I drop the use of ByRef in the function definition and
pass
the params ByVal: 'sOutPut = MyFunction((Data1), (Data2))' , might this
help
save memory/resources, espiceally if this call is used many times in a
single
page?

Any insight into this would be most helpful, any links even better!

It depends on the size of the parameters. ByRef will incur very minor
overhead dereferencing the ByRef parameter, so I only use it for longs and
integers if I need the callee to be able to alter the value.

Otoh, assuming ByVal causes a copy to be made, it would be more expensive in
terms of both cpu and memory to pass a large string by value.

Objects cannot be passed by value in the classic sense, the ByVal modifier
causes something else to happen, (that never seemed intuitive to me, and I
can't seem to find any docs about it.)


-Mark
 
P

Patrice

Objects are basically a pointer. Using ByVal you pass a pointer, using ByRef
a pointer to a pointer. In both cases you can reach the object that is then
updatable. Additionay using ByRef you can assign a new object to the pointer
and reflect this change in the caller...

I'm not talking specifically about VBScript (could have specific things as
it uses the "variant" datatype) but rather about VB /VB.NET...

Patrice
 
M

Mark J. McGinty

Patrice said:
Objects are basically a pointer. Using ByVal you pass a pointer, using
ByRef
a pointer to a pointer. In both cases you can reach the object that is
then
updatable. Additionay using ByRef you can assign a new object to the
pointer
and reflect this change in the caller...

I'm not talking specifically about VBScript (could have specific things as
it uses the "variant" datatype) but rather about VB /VB.NET...

That makes sense.

What is the default for objects, any idea? Guessing from observation, it
*looks* like it's ByRef for both objects and variants of object sub-type in
VB, but always ByVal in VBS?

Thanks,
Mark
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top