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