private sub button1_click(sender as object,e as eventargs)handles button1.click dim a as int32 a=100 Add1(a) msgbox ("a的值為:" & a) '示:a的值為100 Add2(a) msgbox ("a的值為:" & a) '示:a的值為200,因為Add2中的參數(shù)no為ByRef,即 '地址傳遞,因此在Add2中對no進行修改后,將會導(dǎo)致 '參數(shù)a的值也被修改。 End Sub
<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>
======================= 結(jié)果: 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 '=================================================================== ' 參數(shù)傳遞 ' 1.值傳遞參數(shù) (Call By Value) ' Function TestValue(ByVal A,ByVal B) ' 函數(shù)內(nèi)參數(shù) A、B 改變 不影響 函數(shù)的外部變量 ' ' 2.指針參數(shù) (Call By Address) ' Function TestAddress(ByRef A,Byref B) ' 函數(shù)內(nèi)參數(shù) A、B 改變 影響到 函數(shù)的外部變量 ' ' 說明: ' 1. 參數(shù)可以是數(shù)字、字符、數(shù)組、對象等VBSCRIPT語言所支持的大部分類型 ' 2. 函數(shù)返回值的類型也可以是數(shù)字、字符、數(shù)組、對象等VBSCRIPT語言所支持的大部分類型 ' 3. 過程調(diào)用參數(shù)方法與函數(shù)類似 '=================================================================== 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 "函數(shù)(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 "函數(shù)(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 ' 類似,傳遞數(shù)組、對象(或者在函數(shù)中改變其值、屬性) '對象直接把對象名作為參數(shù)即可 ' 數(shù)組,把數(shù)組名稱作為參數(shù) redim aryTest(2,2) dim intNum function Ary_Test(ByRef A) Ary_Test = Ubound(Ary_Test,2) end function '調(diào)用 intNum = Ary_Test(intNum) '值為 3 %>