ByVal 關鍵字

方法引數(或沒有關鍵字以 ByVal 被預設假定的)之前 ByVal 關鍵字說,引數將在某種程度上被髮送允許改變(設定新的值)的變數引數的基礎的方法。
如果它是一個類,它不會阻止引數的內容(或狀態)被更改。

Class SomeClass
    Public Property Member As Integer
End Class

Module Program
    Sub Main()
        Dim someInstance As New SomeClass With {.Member = 42}

        Foo (someInstance)
        ' here someInstance is not Nothing (still the same object)
        ' but someInstance.Member is -42 (internal state can still be changed)

        Dim number As Integer = 42
        Foo(number)
        ' here number is still 42
    End Sub

    Sub Foo(ByVal arg As SomeClass)
        arg.Member = -arg.Member ' change argument content
        arg = Nothing ' change (re-assign) argument
    End Sub    

    Sub Foo(arg As Integer) ' No ByVal or ByRef keyword, ByVal is assumed
        arg = -arg
    End Sub
End Module