按地址 [英] Byref

查看:45
本文介绍了按地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我已经尝试使用byref关键字将

参数传递给我的ASP页面中的子程序和函数,使用VBScript,

但似乎byref和byval都是不相关的,因为简单的变量和数组总是按值和对象传递(我试过

字典的)总是通过引用传递。这是对的吗?或者我错了
我的测试代码出错了(你可以在下面找到)?

有人可以向我解释为什么会出现这种行为?然后,

在哪里byref用过的?谢谢,


Alessio


<%

子方块(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
每个k的
d

d.item(k)= d.item(k)* d.item(k)

下一页

end Sub

Sub incrementaD(byref d)

dim k

每个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.Wr ITE(QUOT; B =" &安培; b& "#< br>")

incrementa(b)

Response.Write(" b ="& b&"#< br>")

dim arr

arr =数组(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" &安培;我,我

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

%>

解决方案

IIRC默认为ByRef。我通常明确指定byref或byval

以确保并作为文档辅助工具。对象和数组可能会忽略出现

总是传递给ByRef,因为变量是对象的引用。

我没有尝试过这个但是你可以尝试传递一个对象

byval并将参数设置为sub中的另一个对象。然后看看它是否更改了



你没有包含测试脚本的输出所以没有办法看到

如果它的行为符合预期。首先尝试简单的事情:


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


按值调用(n)

Response.write" n =" &安培; CStr(n)& " br>"


通过引用调用(n)

Response.write" n =" &安培; CStr(n)& < br>"


你应该得到:

n = 1

n = 1

n = 3


-

Mark Schupp


" Logico" <是***** @ dsi.unive.it>在消息中写道

news:ff ************************** @ posting.google.c om ...

大家好,我已经尝试使用byref关键字将
参数传递给我的ASP页面中的子程序和函数,使用VBScript,
但似乎两者都是byref和byval是不相关的,因为简单的变量和数组总是通过值和对象传递(我试过
字典的)总是通过引用传递。这是对的吗?或者我错了,我的测试代码出错了(你可以在下面找到)?
有人可以向我解释为什么会有这种行为?然后,在哪里是byref。用过的?谢谢,

Alessio

<%
子方格(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)未使用(a)
a(k)= a(k)* a(k)
next
Sub incrementaA(byref a) dim k
for k = lbound(a)to ubound(a)
a(k)= a(k)+ 10
next
end

sub squareD(byval d)
dim k
每个k in d
d.item(k)= d.item(k)* d.item(k)
下一个
结束子增量aD(byref d)
dim k
每个k在d
d.item(k)= d.item(k )+ 10
下一页

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

dim ar r
arr =数组(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" &安培;我,我
下一页
Response.Write(" dict.item(''K2'')="& dict.item(" K2")&"#< br> <)
squareD(dict)
Response.Write(" dict.item(''K2'')="& dict.item(" K2")&" #< br>")
incrementaD(字典)
Response.Write(" dict.item(''K2'')="& dict.item(" K2") &"#< br>")
设置dict = nothing
%>



Logico写道:< blockquote class =post_quotes>大家好,我试过用byref关键字将
参数传递给我的ASP页面中的子程序和函数用VBScript,
但似乎是byref和byval是不相关的,因为简单的变量和数组总是通过值和对象传递(我试过
字典的)总是通过引用传递。这是对的吗?或者我错了,我的测试代码出错了(你可以在下面找到)?
有人可以向我解释为什么会有这种行为?然后,在哪里是byref。用过的?谢谢,

squareD(b)




传递参数时使用括号强制传递参数

byval(简化说明)。在拨打潜水艇时请勿使用括号:


squareD b


欲了解更多信息,请访问:

http://blogs.msdn.com/ ericlippert / ar ... / 15 / 52996.aspx


Bob Barrows


-

Microsoft MVP - ASP / ASP.NET

请回复新闻组。我的From

标题中列出的电子邮件帐户是我的垃圾邮件陷阱,因此我不经常检查它。通过发布到新闻组,您将获得更快的回复。


嘿,


我还有一个关于这个问题的另一个问题,当使用ByRef参数时,

特别是在使用VBScript的ASP中,使用ByRef比使用ByVal更昂贵

params ... ie:如下...


sOutPut = MyFunction(Data1,Data2)


公共函数MyFunction(ByRef Param1,ByRef Param2)

一些代码

...

End Sub


....如果Param1& Param2不会在函数内部改变

调用然后我应该在函数定义中放弃使用ByRef并传递

params ByVal:'sOutPut = MyFunction ((Data1),(Data2))'',这可能有帮助

节省内存/资源,特别是如果在一个

页面中多次使用此调用?


任何洞察这将是最有帮助的,任何链接甚至更好!


干杯。


StephenMcC

..


" Bob Barrows [MVP]"写道:

Logico写道:

大家好,我试过用byref关键字将
参数传递给子程序和函数我的ASP页面有VBScript,
但似乎byref和byval都不相关,因为简单的变量和数组总是通过值和对象传递(我试过
字典的)总是通过引用。这是对的吗?或者我错了,我的测试代码出错了(你可以在下面找到)?
有人可以向我解释为什么会有这种行为?然后,在哪里是byref。用过的?谢谢,

squareD(b)



在传递参数时使用括号强制参数传递
byval(简化说明)。调用子时不要使用括号:

squareD b

有关详细信息,请参阅此处:

http://blogs.msdn.com/ericlippert/ar ... / 15 /52996.aspx

Bob Barrows

-
Microsoft MVP - ASP / ASP.NET
请回复新闻组。我的From
标题中列出的电子邮件帐户是我的垃圾邮件陷阱,因此我不经常检查它。通过发布到新闻组,您将得到更快的回复。



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

解决方案

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

--
Mark Schupp

"Logico" <am*****@dsi.unive.it> wrote in message
news:ff**************************@posting.google.c om...

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



Logico wrote:

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/ar.../15/52996.aspx

Bob Barrows

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don''t check it very often. You will get a
quicker response by posting to the newsgroup.


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

"Bob Barrows [MVP]" wrote:

Logico wrote:

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/ar.../15/52996.aspx

Bob Barrows

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don''t check it very often. You will get a
quicker response by posting to the newsgroup.



这篇关于按地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆