Results 1 to 4 of 4

Thread: What is ByVal and ByRef means?

  1. #1
    Join Date
    Feb 2009
    Posts
    7

    What is ByVal and ByRef means?

    Hi,

    What is ByVal and ByRef means?

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: What is ByVal and ByRef means?

    The word ByVal is short for "By Value". What it means is that you are passing a copy of a variable to your Subroutine. You can make changes to the copy and the original will not be altered.

    ByRef is the alternative. This is short for By Reference. This means that you are not handing over a copy of the original variable but pointing to the original variable. Let's see a coding example.

    Add a new button the form you created in the previous section. Double click the button and add the following code:

    Code:
    Dim Number1 As Integer
    
    Number1 = 10
    Call IncrementVariable(Number1)
    
    MsgBox(Number1)
    You'll get a wiggly line under IncrementVariable(Number1). To get rid of it, add the following Subroutine to your code:

    Code:
    Private Sub IncrementVariable(ByVal Number1 As Integer)
    Number1 = Number1 + 1
    End Sub
    When you're done, run the program and click your new button. What answer was displayed in the message box?

    It should have been 10. But hold on. Didn't we increment the variable Number1 with this line?

    Code:
    Number1 = Number1 + 1
    So Number1 started out having a value of 10. After our Sub got called, we added 1 to Number1. So we should have 11 in the message box, right?

    The reason Number1 didn't get incremented was because we specified ByVal in the Sub:

    Code:
    ByVal Number1 As Integer
    This means that only a copy of the original variable got passed over. When we incremented the variable, only the copy got 1 added to it. The original stayed the same - 10.

    Change the parameter to the this:

    Code:
    ByRef Number1 As Integer
    Run your programme again. Click the button and see what happens.

    This time, you should see 11 displayed in the message box. The variable has now been incremented!

    It was incremented because we used ByRef. We're referencing the original variable. So when we add 1 to it, the original will change.

    The default is ByVal - which means a copy of the original variable. If you need to refer to the original variable, use ByRef.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: What is ByVal and ByRef means?

    If you pass an argument by reference when calling a procedure, the procedure access to the actual variable in memory. As a result, the variable's value can be changed by the procedure.
    If you pass an argument by value when calling a procedure, the variable's value can be changed with in the procedure only outside the actual value of the variable is retained.

    Passing by reference is the default in VBA. If you do not explicitly specify to pass an argurment by value, VBA will pass it by reference.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: What is ByVal and ByRef means?

    The working of ByVal & ByRef in Vb.Net

    As we all know we can pass the variable either ByVal or ByRef.

    Ok, Let have close look at the following code snippet.

    I have class TestClass as shown below


    Public Class TestClass
    Public str As String = "Hai i am the original...."
    End Class


    'In the form place a command button(lets call that as Button1) and paste the following code in the form.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim tes As New TestClass()
    PassByVal(tes)
    MsgBox(tes.str)
    End Sub
    Private Sub PassByVal(ByVal testobj As TestClass)
    testobj.str = "String is changed"
    End Sub


    Now what will be displayed in the message box.

    We all know that when a parameter is passed byval a copy of it is sent and whatever changes we make will not affect the original.

    So, Now Take some time and tell what will be the output.

    ****************************************************

    If you have say "Hai i am the original...." will be displayed. You are wrong.

    If you have chosen correctly, no problem.

    Actually it shows "String is changed".

    Explanation,

    Because, even if you pass an object as a ByVal parameter, it is passed internally as ByRef.

    Since we are passing an object of TestClass . Even though the function PassByVal takes it as the ByVal parameter, it will be passed as ByRef.

Similar Threads

  1. What does error code 503 means?
    By archer in forum Technology & Internet
    Replies: 4
    Last Post: 29-03-2010, 06:13 PM
  2. What does at @ identifier means in C#
    By Shaan12 in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 01:29 AM
  3. What does a HDTV means
    By xanix in forum Monitor & Video Cards
    Replies: 3
    Last Post: 29-07-2009, 04:22 PM
  4. What is prefetch? What does it means
    By AMISH in forum Operating Systems
    Replies: 3
    Last Post: 27-07-2009, 09:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,056,294.91008 seconds with 16 queries