ARTICLE

Using pass by value and pass by reference in VB.NET

Posted by Rohatash Kumar Articles | Visual Basic 2010 September 29, 2010
This article shows pass by Value and pass by reference in VB.NET.
 
Reader Level:

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

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor