locked
FAQs: 传值和传引用之间的区别是什么? RRS feed

答案

  • 当编写子程序或函数时,你可以将变量从主代码段传给他们。

    如果你想通过ByVal语法来传递变量值:将变量的值传递过去而不是传引用,所有在自方法或函数中对此传入变量所作的改动都不会影响到主代码段。如果你不用ByVal或者ByRef对传入变量加以修饰的话,这是默认的传送机制。

    如果你想将在自方法或函数中对传入变量所作改动返回到主代码段,则使用ByRef语法修饰传入变量。这将会传递对变量的引用并允变量值被修改并返回到主代码段。

     

    关于传值和传引用的传参方式的更多信息请参看MSDN文档:

    http://msdn.microsoft.com/en-us/library/ddck1z30.aspx

     

    请查看下面的代码段及其输出。代码声明了一个值类型变量和一个引用类型变量。然后将使用ByValByRef关键词修饰的变量传送到不同子方法中:


    Imports System.Security

    Imports System.Security.Permissions

    Imports System.IO

     

    Public Class Form1

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

        Handles Button1.Click

            Dim parameter_valtype As Integer = 10

            Dim parameter_refertype As demo = New demo(1, "initial value")

     

            Console.WriteLine("the value of value type variable parameter_valtype:" & _

                              parameter_valtype.ToString)

            Console.WriteLine("pass value type variable parameter_valtype with byval modifier")

            passByval_valType(parameter_valtype)

            Console.WriteLine("the value of value type variable parameter_valtype:" & _

                              parameter_valtype.ToString)

            Console.WriteLine("")

            Console.WriteLine("pass value type variable parameter_valtype with byref modifier")

            passByref_valType(parameter_valtype)

            Console.WriteLine("the value of value type variable parameter_valtype:" & _

                              parameter_valtype.ToString)

            Console.Write(vbCrLf)

     

     

            Console.WriteLine("the value of reference type variable parameter_refype: " & _

                             parameter_refertype.nummember.ToString & "  " & _

                             parameter_refertype.strmember)

            Console.WriteLine("pass reference type variable parameter_valtype with byval modifier" & _

                              " and modify the reference variable ")

            passByval_refType_modifyvarialble(parameter_refertype)

            Console.WriteLine("the value of reference type variable parameter_refype: " & _

                               parameter_refertype.nummember.ToString & "  " & _

                               parameter_refertype.strmember)

            Console.Write(vbCrLf)

     

            Console.WriteLine("the value of reference type variable parameter_refype: " & _

                              parameter_refertype.nummember.ToString & "  " & _

                              parameter_refertype.strmember)

            Console.WriteLine("pass reference type variable parameter_valtype with byval modifier")

            passByval_refType(parameter_refertype)

            Console.WriteLine("the value of reference type variable parameter_refype: " & _

                               parameter_refertype.nummember.ToString & "  " & _

                               parameter_refertype.strmember)

            Console.Write(vbCrLf)

     

            Console.WriteLine("the value of reference type variable parameter_refype: " & _

                              parameter_refertype.nummember.ToString & "  " & _

                              parameter_refertype.strmember)

            Console.WriteLine("pass reference type variable parameter_valtype with byref modifier")

            passByref_refType(parameter_refertype)

            Console.WriteLine("the value of reference type variable parameter_refype: " & _

                               parameter_refertype.nummember.ToString & "  " & _

                               parameter_refertype.strmember)

            Console.Write(vbCrLf)

     

            Console.WriteLine("the value of reference type variable parameter_refype: " & _

                             parameter_refertype.nummember.ToString & "  " & _

                             parameter_refertype.strmember)

            Console.WriteLine("pass reference type variable parameter_valtype with byref modifier" & _

                              " and modify the reference variable ")

            passByref_refType_modifyvarialble(parameter_refertype)

            Console.WriteLine("the value of reference type variable parameter_refype: " & _

                               parameter_refertype.nummember.ToString & "  " & _

                               parameter_refertype.strmember)

            Console.Write(vbCrLf)

     

        End Sub

        Public Sub passByref_valType(ByRef i As Integer)

            i = i + 2

        End Sub

        Public Sub passByval_valType(ByVal i As Integer)

            i = i + 4

        End Sub

        Public Sub passByval_refType(ByVal d As demo)

            d.nummember = d.nummember * 2

            d.strmember = "passByval_refType"

        End Sub

        Public Sub passByval_refType_modifyvarialble(ByVal d As demo)

            d = New demo(11, "passByval_refType_modifyvarialble")

        End Sub

        Public Sub passByref_refType(ByRef d As demo)

            d.nummember = d.nummember * 2

            d.strmember = "passByref_refType"

        End Sub

        Public Sub passByref_refType_modifyvarialble(ByRef d As demo)

            d = New demo(d.nummember * 2, "passByref_refType_modifyvarialble")

        End Sub

    End Class

    Public Class demo

        Private _strmember As String = String.Empty

        Private _nummember As Integer = 0

        Public Sub New(ByVal num As Integer, ByVal str As String)

            _strmember = str

            _nummember = num

        End Sub

        Public Property strmember() As String

            Get

                Return _strmember

            End Get

            Set(ByVal value As String)

                _strmember = value

            End Set

        End Property

        Public Property nummember() As Integer

            Get

                Return _nummember

            End Get

            Set(ByVal value As Integer)

                _nummember = value

            End Set

        End Property

    End Class

     

    相关帖子: 

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/2b8825b6-d522-4d8e-83a9-d664d740570b/


    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    MSDN 论坛好帮手 立刻免费下载  MSDN 论坛好帮手
    2011年3月2日 11:42