时间:2021-07-01 10:21:17 帮助过:101人阅读
文件名称: |
ByVal.asp |
ByRef.asp |
具体代码: |
<% |
<% |
运行结果: |
5 |
6 |
结 论: |
注意:子程序TestBy(ByVal T)中T变量声明方式是ByVal 运行结果子程序没有影响到A的值 |
注意:子程序TestBy(ByRef T)中T变量的声明方式是ByRef 运行结果A的值通过子程序发生了改变 |
<SCRIPT LANGUAGE="vbScript">
dim a
a=0
document.write "a=0"
document.write "<br/>sub change(byref ar)<br/>"
change a
document.write a
a=0
document.write "<br/>sub change2(ByVal ar)<br/>"
change2 a
document.write a
a=0
document.write "<br/>sub change3( ar)<br/>"
change3 a
document.write a
a=0
document.write "<br/>function change4(byref ar)<br/>"
change4 a
document.write a
a=0
document.write "<br/>function change5(ByVal ar)<br/>"
change5 a
document.write a
a=0
document.write "<br/>function change6( ar)<br/>"
change6 a
document.write a
a=0
sub change(byref ar)
ar=111
end sub
sub change2(ByVal ar)
ar=222
end sub
sub change3( ar)
ar=333
end sub
function change4(byref ar)
ar=444
end function
function change5(ByVal ar)
ar=555
end function
function change6( ar)
ar=666
end function
</SCRIPT>
=======================
结果:
a=0
sub change(byref ar)
111
sub change2(ByVal ar)
0
sub change3( ar)
333
function change4(byref ar)
444
function change5(ByVal ar)
0
function change6( ar)
666
说明vbs默认是byRef,这点和VB一样, 按地址。
范例:
<%@LANGUAGE="VBSCRIPT"%>
<%
Option Explicit
'===================================================================
' 参数传递
' 1.值传递参数 (Call By Value)
' Function TestValue(ByVal A,ByVal B)
' 函数内参数 A、B 改变 不影响 函数的外部变量
'
' 2.指针参数 (Call By Address)
' Function TestAddress(ByRef A,Byref B)
' 函数内参数 A、B 改变 影响到 函数的外部变量
'
' 说明:
' 1. 参数可以是数字、字符、数组、对象等VBSCRIPT语言所支持的大部分类型
' 2. 函数返回值的类型也可以是数字、字符、数组、对象等VBSCRIPT语言所支持的大部分类型
' 3. 过程调用参数方法与函数类似
'===================================================================
Dim A1,B1
Dim A2,B2
Function TestValue(ByVal A,ByVal B)
A = A + 1
B = B + 1
TestValue = A + B
End Function
Function TestAddress(ByRef A,Byref B)
A = A + 1
B = B + 1
TestAddress = A + B
End Function
A1 = 11
B1 = 33
A2 = 11
B2 = 33
Response.Write "初值:" & " "
Response.Write "A1=" & A1 & " "
Response.Write "B1=" & B1 & "<BR>"
Response.Write "函数(TestValue)值:" & TestValue(A1,B1) & "<BR>"
Response.Write "终值:" & " "
Response.Write "A1=" & A1 & " "
Response.Write "B1=" & B1 & "<BR><BR><BR>"
Response.Write "初值:" & " "
Response.Write "A2=" & A2 & " "
Response.Write "B2=" & B2 & "<BR>"
Response.Write "函数(TestAddress)值:" & TestAddress(A2,B2) & "<BR>"
Response.Write "终值:" & " "
Response.Write "A2=" & A2 & " "
Response.Write "B2=" & B2
'======================
' 相似过程
'======================
Sub Test_Value(ByVal A,ByVal B)
A = A + 1
B = B + 1
End Sub
Sub Test_Address(ByRef A,Byref B)
A = A + 1
B = B + 1
End Sub
' 类似,传递数组、对象(或者在函数中改变其值、属性)
'对象直接把对象名作为参数即可
' 数组,把数组名称作为参数
redim aryTest(2,2)
dim intNum
function Ary_Test(ByRef A)
Ary_Test = Ubound(Ary_Test,2)
end function
'调用
intNum = Ary_Test(intNum) '值为 3
%>