Byref and Byval

Posted on 2006-04-27
Last Modified: 2006-11-18
I would like someone to give me a simple and clear illustration of using Byval and Byref in VB example.

Question by:jskfan
    LVL 69

    Accepted Solution

    Hi jskfan,

    Consider this code snippet:
    Private Sub Command1_Click()
    Dim i As Integer
    Dim j As Integer
        i = 0
        j = 0
        Call Test(i, j)
        MsgBox "i=" & i & vbCrLf & "j=" & j
    End Sub

    Private Sub Test(ByVal p1 As Integer, ByRef p2 As Integer)
        p1 = 10
        p2 = 20
    End Sub

    2 variables (i and j) are created and initialized to 0.
    Then both variables are passed to the Test method:
    -p1 receives a copy of the value contained in i (a 0 value) and creates a new place in memory to store this value.
    -p2 receives a reference (it is a pointer) to the j variable.

    when p1 is modified, the copy is modified but the original remains untouched.
    when p2 is modified, since p2 is a reference to the original place in memory where the value of j is stored, the original value is modified.

    that's why the msgbox prints i=0 and j=20

    Author Comment

    I guess I figured it out ...

    Private Sub calculatesum(ByVal numb1 As Integer, ByVal numb2 As Integer, ByVal totalsum As Integer)
            totalsum = totalsum + numb1 + numb2
        End Sub

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim x, y, z
            x = 5
            y = 5
            z = 7
            Me.calculatesum(x, y, z)

        End Sub

    Author Comment

    thanks, your example is much more simple and clear.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    hasOne  challenge 59 66
    count7 challenge 12 53
    allStar challenge 1 27
    countPairs challenge 7 39
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now