asp中Byval與Byref的區(qū)別
最近看很多成熟的cms系統(tǒng)中就用ByVal
先看下面的例子
文件名稱: | ByVal.asp | ByRef.asp |
具體代碼: | <% Sub TestMain() Dim A : A=5 Call TestBy(A) Response.write A End Sub Sub TestBy(ByVal T) T=T+1 End sub call TestMain() %> |
<% Sub TestMain() Dim A : A=5 Call TestBy(A) Response.write A End Sub Sub TestBy(ByRef T) T=T+1 End sub call TestMain() %> |
運行結(jié)果: | 5 | 6 |
結(jié) 論: | 注意:子程序TestBy(ByVal T)中T變量聲明方式是ByVal 運行結(jié)果子程序沒有影響到A的值 | 注意:子程序TestBy(ByRef T)中T變量的聲明方式是ByRef 運行結(jié)果A的值通過子程序發(fā)生了改變 |
看完了,上面的比較就知道說明意思了吧。
1、引用參數(shù)(ref)在可以作為引用參數(shù)在函數(shù)成員調(diào)用中傳遞之前,必須已明確賦值,而輸出參數(shù)(out)在可以作為輸出參數(shù)在函數(shù)成員調(diào)用中傳遞之前不一定要明確賦值,在該函數(shù)成員正常返回前都必須已明確賦值。
2、在函數(shù)內(nèi)部,引用參數(shù)(ref)被視為初始已賦值,輸出參數(shù)(out)被視為初始未賦值。
3、默認(rèn)地,C#中的所有參數(shù)都是值傳遞。只有在參數(shù)的修飾符中明確包含out或ref,才是引用傳遞。但是需要知道的是,當(dāng)參數(shù)的類型是引用類型時,你傳遞的是一個對象的引用而不是實際的對象。
實例:
sub Add1(ByVal no as int32) no=no+100 end sub sub Add2(ByRef no as int32) no=no+100 end sub 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進(jìn)行修改后,將會導(dǎo)致 '參數(shù)a的值也被修改。 End Sub
ByVal是傳遞值 源數(shù)據(jù)不會被修改,你可以把這個值當(dāng)作自己的局部變量來使用;ByRef是傳遞地址,源數(shù)據(jù)可能被修改,你對這個變量的操作將對你傳入的那個變量產(chǎn)生影響,就像指針的感覺。
在ASP編程中,經(jīng)常需要自己編寫一些函數(shù)(或過程)來實現(xiàn)某些特定的功能,這時往往需要向函數(shù)(或過程)傳遞相應(yīng)的參數(shù)
在函數(shù)(或過程)中進(jìn)行數(shù)據(jù)處理,即有可能需要保留或改變參數(shù)的值,下面有相關(guān)范例
用下面的函數(shù)(TestAddress)就可以使一個函數(shù)多個返回值成為可能(一個函數(shù)返回值,多個參數(shù)改變后的值)
1、ByVal傳值:一種將參數(shù)值而不是將地址傳遞給過程的方式,這就使過程訪問到變量的復(fù)本。結(jié)果,過程不可改變變量的真正值。
2、ByRef傳值:一種將參數(shù)地址而不是將值傳遞給過程的方式,這就使過程訪問到實際的變量。結(jié)果,過程可改變變量的真正值。除非另作說明,否則按地址傳遞參數(shù)。
3、系統(tǒng)默認(rèn)的是ByRef傳值。
例子:
<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默認(rèn)是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 %>
例如:
function makeContent(Byval contentID, Byval page, Byval isMakeHtml)
到此這篇關(guān)于asp中Byval與Byref的區(qū)別的文章就介紹到這了,更多相關(guān)Byval與Byref內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ASP編程入門進(jìn)階(二十):ADO組件之刪除數(shù)據(jù)記錄
ASP編程入門進(jìn)階(二十):ADO組件之刪除數(shù)據(jù)記錄...2007-01-01asp Chr 函數(shù) 數(shù)字轉(zhuǎn)字母的方法
asp Chr 函數(shù) 數(shù)字轉(zhuǎn)字母的方法...2007-08-08對象標(biāo)記具有無效的 ''MSWC.MyInfo'' ProgID
對象標(biāo)記具有無效的 ''MSWC.MyInfo'' ProgID...2007-02-02