ByVal and ByRef with reference type

前端 未结 3 1309
一个人的身影
一个人的身影 2020-12-02 02:04

Please see the code below:

Public Class TypeTest
    Public variable1 As String
End Class

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Obj         


        
3条回答
  •  执笔经年
    2020-12-02 02:35

    ByVal actually copies the value of the current variable and pass it to the function. By reference copies the current reference to the function. Let's take your example:

    t1 is a reference variable which contains the address of the object of type TypeTest, so when you use ByVal, the address is copied to the function. Eventually you use the same object.

    Where it really makes sense is, in case of basic variables like int, float etc.

    Example: Int temp =0;

    temp is a variable which contains the value 0, so when copied 0 is passed. If you use reference then address of temp is passed (&0) to the function.

    To summarize, ByRef makes more sense only in the basic types and not complex types.

提交回复
热议问题