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