locked
What is the difference between ByVal and ByRef? RRS feed

Answers

  • When you write a subroutine or function, you can pass variables from your main code to that subroutine or function.

     

    If you want to pass the value of the variable, use the ByVal syntax. By passing the value of the variable instead of a reference to the variable, any changes to the variable made by code in the subroutine or function will not be passed back to the main code. This is the default passing mechanism when you don’t decorate the parameters by using ByVal or ByRef.

     

    If you want to change the value of the variable in the subroutine or function and pass the revised value back to the main code, use the ByRef syntax. This passes the reference to the variable and allows its value to be changed and passed back to the main code.

     

    For more information about passing Arguments by Value and by Reference, please refer to MSDN document:

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

     

    Take a look at the following code snippet and its output. The code declares a value-type variable and a reference-type variable. Then pass the variables to the different subroutines which use the ByVal or ByRef keyword to decorate the parameters.

     

    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
    

     

    Related thread:

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

     

    For more FAQ about Visual Basic .NET General, please see Visual Basic .NET General FAQ

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, April 10, 2009 3:22 AM

All replies

  • When you write a subroutine or function, you can pass variables from your main code to that subroutine or function.

     

    If you want to pass the value of the variable, use the ByVal syntax. By passing the value of the variable instead of a reference to the variable, any changes to the variable made by code in the subroutine or function will not be passed back to the main code. This is the default passing mechanism when you don’t decorate the parameters by using ByVal or ByRef.

     

    If you want to change the value of the variable in the subroutine or function and pass the revised value back to the main code, use the ByRef syntax. This passes the reference to the variable and allows its value to be changed and passed back to the main code.

     

    For more information about passing Arguments by Value and by Reference, please refer to MSDN document:

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

     

    Take a look at the following code snippet and its output. The code declares a value-type variable and a reference-type variable. Then pass the variables to the different subroutines which use the ByVal or ByRef keyword to decorate the parameters.

     

    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
    

     

    Related thread:

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

     

    For more FAQ about Visual Basic .NET General, please see Visual Basic .NET General FAQ

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, April 10, 2009 3:22 AM
  • what is point of ask question and answer it, if know the answer dont bother to post it as question, do it as discussion. You want a shortcut to get points.
    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Friday, April 10, 2009 3:54 AM
  • I'm tempted to mark all of these as Abuse because they have filled the first page of REAL questions!
    Doug [Remember to mark replies as helpful or as the answer (if they are)]
    Friday, April 10, 2009 9:02 AM
  • I would think (hope) that because he has MSFT next to the display name, he is a Microsoft employee and has been asked to do it??
    Hobbyist programmer: VB.NET, C++, Pascal
    Friday, April 10, 2009 9:05 AM
  • Clarification: Xiaoyun Li was publishing VB.NET FAQs.

    Thank you All for your enthusiastic support :)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, April 13, 2009 12:11 AM