參數 ByVal 和 ByRef 區別

  1. ByVal 關鍵字的參數是值傳參,ByRef 關鍵字的參數是地址傳參。

  2. ByVal的參數,是指定參數類型的值,不會改變傳參對象的原地址值;

    ByRef的參數,是指定原地址入參,是直接使用原地址對象,如果在方法裏修改該對象的話,原地址的對象值也會改變,使用時慎用。


    例子:


    Public Class Form4

        Private number As Integer = 1


        Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            test2(number)

           MsgBox(number)

            test(number)

            MsgBox(number)

        End Sub


        Private Sub test(ByVal num As Integer)

            num += 1

        End Sub


        Private Sub test2(ByRef num As Integer)

            num += 1

        End Sub

    End Class


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章