Pass by Value and Pass by Reference in VB.NET

This article shows pass by Value and pass by reference in VB.NET.
  • 9836

In this article We will learn How to use pass by Value and pass by Ref in VB.NET.

Pass by value (using ByVal keyword):- value type is passed by value, a copy of that data is passed. The subroutine/function cannot change the value of this variable back in the calling routine.

For example:

Module Module1

    Class passbyvalue

        Public Shared Sub change(ByVal n As Integer)  ' formal argument

            n = n + 100

        End Sub

        Public Shared Sub main()

            Dim n As Integer = 5

            change(n)                ' actual argument

            Console.WriteLine(n)

        End Sub

    End Class

End Module

 

OUTPUT:

 

val.gif
 

Pass by Ref (using ByRef keyword): pass ByRef, Visual Basic passes a pointer to the procedure. Any changes the procedure makes to this variable will effect the original .

 

Module module1

    Class PassByReference

        Public Shared Sub Change(ByRef n As Integer)

            'formal argument

            n = n + 100

        End Sub

        Public Shared Sub Main()

            Dim n As Integer = 5

            Change(n)

            'actual argument

            System.Console.WriteLine(n)

        End Sub

    End Class

End Module

 

OUTPUT:

 

ref.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.